Ans: yes, static data member and static member function, both can be used in the same program. A program with output to show its possibility:
#include<iostream.h>
#include<conio.h>
class don
{
static int d;
public:
don();
static void show();
};
int don::d=0;
don::don()
{
d++;
}
void don::show()
{
cout<<"\nVALUE of d is:"<<d;
}
void main()
{
cout<<"\nBefore instantiation of object of the object";
don::show();
don d1,d2,d3,d4,d5;
cout<<"\nAfter instantiation of the object";
don::show();
getch();
}
Output of this program:
Before instantiation of object of the object
VALUE of d is: 0
After instantiation of the object
VALUE of the d is: 5
Q 2. Mention three Characteristics of a static data member.
ANS: Static data member is normally used to maintain values common to the whole program, three characteristics of a static data member is:
When the first object of any class is created then static data member is initialized to zero.
There should be only one copy of data member is created for the entire class and shared by all the objects of that class. It's no matter that how many object is created in that program.
It can be seen only in the class but it's lifetime is the entire program.
---------------------------------------------------------------------------------------------------------------------
Q 3. Find out errors:
a)
# include<iostream.h>
class abc
{
private:
static int count;
public:
abc()
{
count=10;
}
void display() const
{
cout<<endl<<"count=" <<count;
}
};
void main()
{
abc a1,a2,a3;
a1.display();
a2.display();
a3.display();
}
ANS: errors are:
Definition of static data member is missing. "int static::count;" { the static variable must defined outside the class
Getch() is missing in main.
A header file named "conio.h" is missing which is used for getch().
After reforming these errors the output is:
Count=10
Count=10
Count=10
---------------------------------------------------------------------------------------------------------------------
b)
# include<iostream.h>
class example
{
private:
int data;
public:
example();
void display()const;
};
example::example()
{
data=0;
}
example::display()
{
cout<<endl<< "count=" <<count;
}
void main()
{
example d;
d.display();
}
ANS: errors are:
"example::display" is not a member function of "class example". { "void example::display()const" is required.
"count" is not defined in class.
"getch()" is missing from main.
A header file "conio.h" is required.
After reforming these errors the output is: count=7711.
Replacing "data" with "count", then the output is: count=0.
-------------------------------------------------------------------------------------------------------------------
Q 4. 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:
Program coding:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
friend void comp(complex ,complex);
void getreal();
void getcompl();
};
void complex::getreal()
{
cout<<"ENTER REAL part:";
cin>>a;
}
void complex::getcompl()
{
cout<<"ENTER COMPLEX part:";
cin>>b;
}
void comp(complex x,complex y)
{
cout<<"\n"<<"Thr complex number is:";
cout<<x.a<<"+i"<<y.b;
}
void main()
{
complex a1,a2;
a1.getreal();
a2.getcompl();
comp(a1,a2);
getch();
}
Output of this program:
ENTER REAL part: 5
ENTER COMPLEX PART: 6
The complex number is: 5+i6
---------------------------------------------------------------------------------------------------------------------
PART B
---------------------------------------------------------------------------------------------------------------------
Q 5.How is dynamic initialization of objects achieved?
Ans: By using dynamic constructors a dynamic initialization of object can be achieved. Because constructor is used to allocate memory while creating objects and this leads to initialize value of objects during run time.
With a help of example, how a dynamic initialization of can be achieved:
// The dynamic initialization be explaind by the below procedure as:
#include<iostream.h>
#include<conio.h>
class dynin
{
int rn;
float fees;
public:
dynin(int a,int b);
dynin(dynin & m); //copy constructor
};
dynin::dynin(int a,int b)
{
rn=a;
fees=b;
}
dynin::dynin(dynin & m)
{
rn=m.rn;
fees=m.fees;
cout<<"\n copy constructor at work";
}
void main()
{
float x,y;
cout<<"\nEnter the roll no of student:";
cin>>x;
cout<<"\nenter the fees of the student:";
cin>>y;
dynin m1(x,y); //dynamic initialization of object with value x & y
getch();
}
Output of this program:
Enter the roll no of student:03
Enter the fees of the student: 56000
---------------------------------------------------------------------------------------------------------------------
Q 6. Distinguish between the following two statements
Time T2(T1);
Time T2=T1;
Ans:
In case of parameterized constructor the initial values must be passed at the time of object creation. It can be done by two methods as:
Implicit call constructor is called even when its name has not been mentioned. Ex: time t1(t2)ïƒ t2 is copied to t1.
Explicit call constructor is explicitly provided to invoke it so that object can be initialized. Explicit call allow the user to create a temporary instance or temporary object. Ex: time t1(t2)ïƒ t2 is copied by assignment to t1.
---------------------------------------------------------------------------------------------------------------------
Q 7.WAP to show the order of invocation of the constructor and destructor.
Ans:
Coding of program to show order of invocation:
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
static int c;
public:
abc()
{
c++;
cout<<"\norder default:"<<c;
}
abc(int, int)
{
c++;
cout<<"\norder parameterised:"<<c;
}
~abc()
{
c--;
cout<<"\ndestructor:"<<c;
}
};
int abc::c;
void main()
{
abc a1();
{
abc a2(1,2),abc();
}
abc a4();
getch();
}
Output of this program:
Order default: 1
Order parameterized: 2
Order default: 3
Destructor: 2
Destructor: 1
Order default: 2
---------------------------------------------------------------------------------------------------------------------
Q 8. "Constructor and Destructor are the two sides of a coin". Explain with an
illustration.
Ans: yes, constructor and destructor are two sides of a coin because:
Constructor is used to create or initialize objects but destructor is used to destroy the objects created or initialized by constructor.
Destructor is used for reinitializing the objects which are initialized by constructor.
A constructor function has the same name as class_name that of class also destructor has have same name as class_name but followed by " ~ "(tilde symbol).
General syntax for constructor is:
Class class_name
{
Int a,d;
Public:
Void getdata();
Class_name(); //constructor defined
};
General syntax for destructor is:
Class class_name
{
Int a,d;
Public:
Voidgetdata();
~class_name(); //destructor defined
};
In constructor "new" is used to create memory allocation but in destructor "delete" is used to destroy the allocated memory.
Constructor may be have argument but destructor have no argument.
==============================================================================
XXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
==============================================================================