Wednesday, June 1, 2011

C++ Basic interview questions on class and objects

What is a class?
A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism for creating user-defined data types.
Classes are generally declared using the keyword class, with the following format:
class ExampleClass
{
};
here ExampleClass is the name of the class.

What is an object?
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
class ExampleClass
{
};
int main()
{
ExampleClass obj1; // obj1 is the instance of the class ExampleClass
}

What is Aggregate class?
An aggregate class is a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions.[2] Such a class can be initialized with a brace-enclosed comma-separated list of initializer-clauses.

class Aggregate
{
public:
int a;
double b;
};
int main()
{
Aggregate obj = {5, 10.25};
}

What are the ways of creating a class?
Classes can be defined not only with keyword class, but also with keywords struct and union.
The concepts of class and data structure are so similar that both keywords (struct and class) can be used in C++ to declare classes (i.e. structs can also have function members in C++, not only data members).
The only difference between both is that members of classes declared with the keyword struct have public access by default, while members of classes declared with the keyword class have private access. For all other purposes both keywords are equivalent.
The concept of unions is different from that of classes declared with struct and class, since unions only store one data member at a time, but nevertheless they are also classes and can thus also hold function members. The default access in union classes is public.

What is Constructor?
Constructors in C++ are special member functions of a class. They have the same name as the Class Name. There can be any number of overloaded constructors inside a class, provided they have a different set of parameters. There are some important qualities for a constructor to be noted.

  • Constructors have the same name as the class.

  • Constructors do not return any values.

  • Constructors are invoked first when a class is initialized. Any initializations for the class members, memory allocations are done at the constructor.

  • If the constructor is declared as private, then the class cannot be instantiated.
class Example
{
public:
Example(){
cout<<"i am default constructor\n";
}
Example(int a){
cout<<"i am overloaded constructor\n";
}
};
here Example() is the default constructor.And Example(int a) is an overloaded constructor which takes one interger argument.

What is Destructor?
Destructors in C++ also have the class name, except for the fact that they are preceded by a '~' operator.
The destructors are called when the object of a class goes out of scope.
It is not necessary to declare a constructor or a destructor inside a class. If not declared, the compiler will automatically create a default one for each.
If the destructor is declared as private, then the class cannot be instantiated.
class Example
{
public:
Example(){
cout<<"i am default constructor\n";
}
Example(int a){
cout<<"i am overloaded constructor\n";
}
};

Tell something about C++ union?

  • A C++ union is a limited form of the class type.

  • It can contain access specifiers (public, protected, private), member data, and member functions, including constructors and destructors.

  • It cannot contain virtual functions or static data members.

  • It cannot be used as a base class, nor can it have base classes.

  • Default access of members in a union is public.

What is the difference between C++ class and struct?
There is no difference between a class and a struct except that by default, the members of a struct are public, and the members of a class are private.
Also structs are by default inherited publicly and classes by default are inherited privately. Other than that, whatever you can do in a class, you can do in a struct.

Explain the below code?
struct A
{
int a;
};
class B:A
{
int b;
};
int main()
{
B obj;
obj.a = 10; // compilation error
}
error: `int A::a' is inaccessible
The GNU project C++ compiler is consistent with 11.2.2 of ISO/IEC 14882-2003, which states that the kind of inheritance is determined by the derived class being declared as a class or struct when an access specificer for the base class is absent.

What is the difference between class and object?
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 object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.

  • An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

No comments:

Post a Comment