Static data member are data objects that are common to all the objects of a class. They exist only once in all objects of this class. They are already created before the finite objects of the respective class.
Whenever a static data member is declared and it has only a single copy, it will be shared by all the instance of class. That is, the static member becomes global instances of the class.
We declare the class of a member as static when we want to make a variable common to all the instances of the class and when we want to retain the value throughout the program
Q2. Mention three Characteristics of a static data member?
Ans 2
Classes can contain static member data and member functions. When a data member is declared as static, only one copy of the data is maintained for all objects of the class. (For more information, see Static Member Functions.)
Static data members are not part of objects of a given class type; they are separate objects. As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope. These static members have external linkage
Ans.3
a)
# include<iostream.h>
class abc
{
private:
static int count;
public:
abc()
{
count=10;
}
void display()
{
cout<<endl<<"count=" <<count;
}
};
void main()
{
abc a1,a2,a3;
a1.display();
a2.display();
a3.display();
}
b) # include<iostream.h>
class example
{
private:
int data;
public:
example();
void display();
};
example::example()
{
data=0;
}
example::display()
{
cout<<endl<< "count=" <<count;
}
void main()
{
example d;
d.display();
}
Q4. WAP that defines a class complex (defining a complex number).Write a friend function that takes two argument of class complex and returns a complex number?
Ans 4 Define a class complex that contains two data member to store real and imaginary part of the complex number. Create a function to get values from the keyboard into these complex numbers, overload binary + and - to calculate addition and subtraction of two complex numbers respectively using member function.
#include<iostream.h>
#include<conio.h>
class complex
{
public:
float a,b,c,d,x,y;
void show();
void add();
void sub();
void mul();
};
void complex::show()
{
cout<<"enter real nd imaginary part of 1st no "<<endl;
cin>>a>>b;
cout<<a<<"+i"<<b<<endl;
cout<<"enter real nd imaginary part of 2nd no "<<endl;
cin>>c>>d;
cout<<c<<"+i"<<d<<endl;
}
void complex::add()
{
x=a+c;
y=b+d;
cout<<"the add of no is"<<endl;
cout<<x<<"+i"<<y<<endl;
}
void complex::sub()
{
x=a-c;
y=b-d;
cout<<"sub is "<<endl;
cout<<x<<"+i"<<y<<endl;
}
void complex::mul()
{
x=(a*c)-(b*d);
y=(d*a)+(b*c);
cout<<"the multiplication is"<<endl;
cout<<x<<"+i"<<y<<endl;
}
void main()
{
clrscr();
complex man;
man.show();
man.add();
man.sub();
man.mul();
getch();
}
Ans5. How is dynamic initialization of objects achieved?
. #include<iostream.h>
#include<conio.h>
class abc
{
public:
int a;
int b;
int c;
abc(int x,int y)
{
a=x;
b=y;
}
abc(int x,int y, int z)
{
a=x;
b=y;
c=z;
}
};
void main()
{
clrscr();
abc obj(4,5);
cout<<"the value of a and b is:"<<obj.a<<"\t"<<obj.b;
abc obj1(3,6,7);
cout<<"\n\nthe value of a and b and c is:"<<obj1.a<<"\t"<<obj1.b<<"\t"<<obj1.c;
getch();
}
Q6. Distinguish between the following two statements
Time T2(T1);
Time T2=T1;?
Ans6. In the first statement Time T2 (T1) in this we are passing the address of object T1 to a newly created object T2. And in the class it will fetch the value of initialized variable.
Whereas in second statement Time T2=T1 in this we are copying the constructor T1 to the new created constructor T2. This procedure is called as COPY CONSTRUCTOR.
Q7. WAP to show the order of invocation of the constructor and destructor?
Ans 7
Usually the constructor/s, then the copy constructor/s and and finally the destructor appear at the top of the public: section of the class.
class Cat
{
public:
Cat();//constructor
Cat(Cat&);//copy constructor
~Cat();//destructor;
private:
//private data and methods
};
Q8. "Constructor and Destructor are the two sides of a coin". Explain with an illustration?
Ans.8
Constructors and destructors obey the same access rules as member functions. For example, if you declare a constructor with protected access, only derived classes and friends can use it to create class objects.
The compiler automatically calls constructors when defining class objects and calls destructors when class objects go out of scope. A constructor does not allocate memory for the class object its this pointer refers to, but may allocate storage for more objects than its class object refers to. If memory allocation is required for objects, constructors can explicitly call the new operator. During cleanup, a destructor may release objects allocated by the corresponding constructor. To release objects, use the delete operator.
#include <iostream>
using namespace std;
struct A {
virtual void f() { cout << "void A::f()" << endl; }
virtual void g() { cout << "void A::g()" << endl; }
virtual void h() { cout << "void A::h()" << endl; }
};
struct B : A {
virtual void f() { cout << "void B::f()" << endl; }
B() {
f();
g();
h();
}
};
struct C : B {
virtual void f() { cout << "void C::f()" << endl; }
virtual void g() { cout << "void C::g()" << endl; }
virtual void h() { cout << "void C::h()" << endl; }
};
int main() {
C obj;
}
The following is the output of the above example:
void B::f()
void A::g()
void A::h()