Constructor and Destructor
What is a copy constructor ? Give a suitable example in C++ to illustrate with its definition within a class and a declaration of an object with the help of it.
A copy constructor is an overloaded constructor in which an object of the same class is passed as a reference parameter.
class Point
{
int x;
public:
Point(){
x=0;
}
Point(Point & p) // Copy constructor
{
x = p.x;
}
};
void main()
{
Point p1;
Point p2(p1);//Copy constructor is called here
}
Sponsor Area
Write the definition of a class PIC in C++ with the following description :
Private Members
– Pno //Data member for Picture Number (an integer)
– Category //Data member for Picture Category (a string)
– Location //Data member for Exhibition Location (a string)
– FixLocation //A member function to assign
//Exhibition Location as per category
//as shown in the following table
Category | Location |
Classic | Amina |
Modern | Jim Plaq |
Antique | Ustad Khan |
Public Members
– Enter() //A function to allow user to enter values
//Pno, category and call FixLocation() function
– SeeAll() //A function to display all the data members
Answer the questions (i) to (iv) based on the following:
class Exterior
{
int OrderId;
char Address[20];
protected:
float Advance;
public:
Exterior();
void Book();
void View();
};
class Paint:public Exterior
{
int WallArea,ColorCode;
protected:
char Type;
public:
Paint();
void PBook();
void PView();
};
class Bill : public Paint
{
float Charges;
void Calculate();
public :
Bill();
void Billing();
void Print();
};
(i) Which type of Inheritance out of the following is illustrated in the above example?
– Single Level Inheritance
– Multi-Level Inheritance
– Multiple Inheritance
(ii) Write the names of all the data members, which are directly accessible from the member functions of class Paint.
(iii) Write the names of all the member functions, which are directly accessible from an object of class Bill.
(iv) What will be the order of execution of the constructors, when an object of class Bill is declared?
Sponsor Area
Sponsor Area