C++ Interview Questions

Showing Questions 21 - 40 of 350 Questions
First | Prev | Next | Last Page
Sort by: 
 | 
Jump to Page:
  •  

    What is the difference between class and structure?

    Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private.

    Star Read Best Answer

    Editorial / Best Answer

    sumitv  

    • Member Since Sep-2008 | Sep 4th, 2008


    Structure does support inheritance.

    try out the following code.

    #include<iostream>

    using namespace std;

    struct Base
    {
        int A;
    };

    struct Derived:public Base
    {
        int B;
        void display();
    };

    void Derived::display()
    {
        cout<<endl<<"A = "<<A<<endl;
    }

    int main()
    {
        Derived D;
        D.A = 111;
        D.display();
        getchar();
        return 0;
    }

    Try out private and protected inheritance as well. It works. :)

    Regards,
    Sumit

    Sajeed Ullah

    • Dec 2nd, 2016

    All data members of the class is by default private,
    Whereas all data members of structure is by default public.

    Ashish

    • Mar 24th, 2016

    Beware, whatever is supported by a class is also supported by struct whether its inheritance, polymorphism, encapsulation, data hiding etc. etc. as of C++ 11. Only difference is, for a class default access specifier is private whereas for struct its public.

  •  

    Flowchart

    an electricity board charges the following rates to domestic users to discourage large consumption of energy:
    for the first 100 units - 10 paise per unit 
     for next 200 units - 20 paisa per unit
     beyond 300 units - 30 paisa per unit . if the total cost is more than 10 OMR then an additional surcharge of 15% is added.draw the flowchart and write the algorithm to calculate the...

    Ted

    • Oct 17th, 2016

    1 start
    2 Input U for unit
    3 If U

  •  

    Array in Function

    Write C++ program tell the user
    Press 1 to change Row to Row and change (using Function ) output like that :
    1 1 1
    2 2 2
    3 3 3
    and press 2 to change Row to Col and change (using function) output like that :
    1 2 3
    1 2 3
    1 2 3
    and press 0 to End the Program

    mayank

    • Sep 7th, 2016

    Code
    1. #include <iostream>
    2.  
    3. using namespace std;
    4.  
    5. void rowtorow()
    6. {
    7.     for(int i=0;i<3;++i)
    8.     {
    9.         for(int j=0;j<3;++j)
    10.         {
    11.             cout<<i+1<<" ";
    12.         }
    13.         cout<<"
    14. ";
    15.     }
    16. }
    17. void rowtocol()
    18. {
    19.     for(int i=0;i<3;++i)
    20.     {
    21.         for(int j=0;j<3;++j)
    22.         {
    23.             cout<<j+1<<" ";
    24.         }
    25.         cout<<"
    26. ";
    27.     }
    28. }
    29. void close(){
    30.  
    31. }
    32.  
    33. typedef void (*fp)();
    34. int main()
    35. {
    36.     const fp arr[3]={close,rowtorow,rowtocol};
    37.     int i;
    38.     cout<< " enter the number 0,1,or 2:";
    39.     cin>>i;
    40.     arr[i]();
    41.  
    42.     cout << "
    43. Hello world!" << endl;
    44.     return 0;
    45. }
    46.  

  •  

    Virtual Constructor

    Why constructor cannot be virtual in C++?

    James Thompson

    • Aug 23rd, 2016

    Because the virtual pointer table isnt initialised till after the constructor is complete.

    Aarish

    • Apr 20th, 2016

    Advanced C++ | Virtual Constructor Can we make a class constructor virtual in C++ to create polymorphic objects? No. C++ being static typed (the purpose of RTTI is different) language, it is meaningl...

  •  

    What is the difference between an object and a class?

     Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.  Ø      A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change. Ø       The class to which an...

    Minh Nguyen

    • Jun 27th, 2016

    A class is an user-defined data structure and an object is an instance of that class.

    Jumana iqbal

    • Apr 24th, 2016

    Class is a user degined data type, the variables of class known as object, which are the central focus of object oriented programming

  •  

    What do you mean by inheritance?

    Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.  

    anamika

    • Jun 14th, 2016

    Multiple Inheritance is not used in Java

    shylu

    • Jun 22nd, 2012

    It is a process that one class can acquires the properties of another base class

  •  

    Use of OOPs in C++

    What is the use of object oriented system?

    Aarish

    • Apr 20th, 2016

    An object-oriented operating system is an operating system that uses methods of object-oriented programming. An object-oriented operating system is in contrast to an object-oriented user interface or...

  •  

    Solve this using C++ code

    Write a solution in any language to the following: Given a large list of integers (more than 1 000 000 values) find how many ways there are of selecting two of them that add up to 0

    Jin Hyuk Cho

    • Feb 8th, 2016

    O(n) solution"cpp #include #include #include #include #include #include // std::locale, std::isdigit #include #include #include #include int findTwoSu...

    dada

    • Feb 7th, 2016

    This solution is O(n)"cpp #include #include #include #include #include using namespace std; #define MAX_NUM 10000 int main() { vector in(MAX_NUM); //input data...

  •  

    Permutations

    Write a program to display all possible permutations of a given input string--if the string contains duplicate characters, you may have multiple repeated results. Here is a sample for the input cat
    cat,cta,act,atc,tac,tca

    Jin Hyuk Cho

    • Feb 3rd, 2016

    A working example

    Code
    1. #include <iostream>
    2. #include <string>
    3. #include <set>
    4. #include <sstream>
    5.  
    6. using namespace std;
    7.  
    8. void permute (set<char> &chars, set<string> &permuted) {
    9.     if (chars.size() == 1) {
    10.         stringstream charSS;
    11.         charSS << *(chars.begin());
    12.         permuted.insert(charSS.str());
    13.         return;
    14.     }
    15.     for (set<char>::iterator it = chars.begin(); it != chars.end(); ++it) {
    16.         // constructing remaining characters
    17.         set<char> remainingChars(chars);
    18.         remainingChars.erase(*it);
    19.         // constructing empty set for results
    20.         set<string> permutedRemainingStrings;
    21.         permute(remainingChars, permutedRemainingStrings);
    22.         for (set<string>::iterator rit = permutedRemainingStrings.begin();
    23.              rit != permutedRemainingStrings.end();
    24.              ++rit) {
    25.             stringstream ss;
    26.             ss << *it << *rit;
    27.             string onePermute = ss.str();
    28.             //cout << onePermute << endl;
    29.             permuted.insert(onePermute);
    30.         }
    31.     }
    32. }
    33. int main () {
    34.    
    35.     ostream_iterator<string> output(cout, " " );
    36.     set<char> chars = {c, a, t};
    37.     set<string> permuted;
    38.     permute(chars, permuted);
    39.     copy(permuted.begin(), permuted.end(), output);
    40.     cout << "Done!" << endl;
    41.    
    42.    
    43.     return 0;
    44. }

    neha

    • Aug 27th, 2015

    plz see below code"cpp #include #include using namespace std; void Permutations(string str) { for(int i = 0; i < str.size(); i++) ...

  •  

    Read the following code and then implement the following parts:

    Read the following code and then implement the following parts:

    class Point {
    public int xCoordinate;
    public int yCoordinate;
    }

    1. Derive a class from Point and call it DerivedPoint, the new class will contain two constructors, the first one takes no arguments, it assigns the point to the point (1,1) and the second constructor takes two parameters...

    kladarak

    • Nov 28th, 2015

    danarusus answer was pretty much there, but there are a few things that could be improved with it. - Display() doesnt output x and y coords in exactly the format requested. There should be no space...

    ThePurpleTwist

    • Nov 12th, 2015

    There is a mistake in the code submitted by chandra ;
    DerivedPoint : public Point instead of "::"

  •  

    Can we use static variables in file2 if they are defined in file1 ? If yes, then how ?

    Star Read Best Answer

    Editorial / Best Answer

    Answered by: Kranthi Kiran

    • Dec 2nd, 2006


    We cannot use a 'static variable' declared in one file and use it in file 2.Reason: The purpose of 'static' keyword is to retain the value betwwen the function and to restrict thescope of the variable to the file.When a variable is declared as static it will be given memory in global area portion of the process segments with the variable mname prefixed with the file namefor Example: static int k; implies it is stored as "filename.k" in global segment.so if you try to use it in another file the compiler finds the variable prefix and flags out an error.

    Vineet Srivastava

    • Nov 13th, 2015

    Syntatically you cannot as static is meant for one file access only. But still if you want - you can do it by passing it by reference to another file through some function.

    shreekant

    • Nov 5th, 2015

    Nice explained by Kalayama "We cant have two storage specifiers for a single Variable declaration. If one needs to use the variable in multiple files, then he needs to declare it has extern." Its simple thing.

  •  

    C++ Program to output the sum of positive numbers

    WAP to output the sum of positive numbers, sum of negative numbers, sum of even numbers,
    sum of odd numbers from a list of numbers entered by the user. The list terminates when the
    number entered is zero.

    JOYJIT DEB

    • Jul 19th, 2015

    "cpp #include using namespace std; int main(){ signed int num; static unsigned int l_SumOfPos = 0; static signed int l_SumOfNeg = 0; static...

  •  

    What is public, protected, private?

    Public, protected and private are three access specifiers in C++. 1. Public data members and member functions are accessible outside the class.2. Protected data members and member functions are only available to derived classes.3. Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.

    ankit

    • Jun 26th, 2015

    Can we use protected specifier first than public than private in a class definition in c++ by making class

  •  

    What is the difference between get and getline ?

    mysterysource

    • May 31st, 2015

    If i am not wrong get() reads char by char and it does not read the blank spaces

    anabarai

    • Jul 4th, 2011

    get() extracts char by char from a stream and returns its value (casted to an integer) whereas getline() is used to get a line from a file line by line. Normally getline is used to filter out delimite...

  •  

    What is virtual constructors/destructors?

    Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object.   There is a simple solution to this problem – declare a virtual base-class destructor. This makes all derived-class destructors virtual even...

    Eric Nantel

    • Apr 23rd, 2015

    There is no virtual constructor !

    Tausif

    • Nov 29th, 2014

    Constructor can be virtual function because constructor get invoked whenever an object is created, which take place at compile time. But in the case of virtual function binding takes place at the run time.

  •  

    Write a program that calculates and prints the bill ?

    write a program that calculates and prints the bill for Viti Telephone Company. The company offers two types of services: regular and premium: its rate very depending on the type of service.

    The rates are computed as follows:

    Regular Service
    $10.00 plus first 50 minutes are free.
    Charges for over 50 minutes are $0.20 per minute

    Premium Service

  •  

    Binary

    Write a program that continues to ask the user to enter any decimal number and calculate it’s binary,octal and hexadecimal equivalent.

    Badugu

    • Mar 11th, 2015

    Please try following:

    Code
    1. #include<iostream>
    2. #include<fstream>
    3. #include <bitset>
    4.  
    5. using namespace std;
    6.  
    7. int main()
    8. {
    9.     int i,j;
    10.     cout<<"Enter a decimal Number:";
    11.     while (cin>>i) {
    12.                  
    13.           cout<<"Hexa Decimal:"<<std::hex<<i<<"     Octal:"<<std::oct<<i<<endl;
    14.           std::bitset<8> x(i);
    15.           cout<<"Binary:"<<x<<endl;
    16.           cout<<"Enter a decimal Number:";
    17.           }
    18.          
    19.           return 0;
    20.           }
    21.  

    rohit suri

    • Sep 11th, 2014

    Code
    1.  
    2. int binary_function(int num)
    3. {
    4.    if(num==1)
    5.       return 1;
    6.   else
    7.     return binary_function(num/2)*10+(num%2);
    8. }
    9.  

  •  

    What is significance of calloc function in C?

    Sumit

    • Mar 5th, 2015

    Its mainly used to allocate memory in blocks

    Manish Rai

    • Nov 19th, 2014

    1. it is used to allocate memory for an array. 2. it also sets allocated memory to 0 which malloc dont.

    Code
    1. int* a = (int*) calloc(10, sizeof(int));  // where a sets to 0
    2. int* a = (int*) malloc(10* sizeof(int));  // where a is uninitialized

Showing Questions 21 - 40 of 350 Questions
First | Prev | Next | Last Page
Sort by: 
 | 
Jump to Page: