1.What is a copy constructor? Illustrate with a suitable C++ example.
A copy constructor is an overloaded constructor in which an object of the
same class is passed as reference parameter
#include<iostream>
class Point
{
private:
int x, y;
public:
Point(int x1, int y1)
{ x = x1; y = y1; }
// Copy constructor
Point(const Point &p2)
{x = p2.x; y = p2.y; }
int getX()
{ return x; }
int getY()
{ return y; }
};
int main()
{
Point p1(5, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
2.What is constructor ? Give the example
Constructors are special class functions which performs initialization of every object.The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object.
Types of Constructors in C++
Constructors are of three types:
1.Default Constructor
Default constructor is the constructor which doesn't take any argument. It has no parameter.
2.Parametrized Constructor
It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created.
To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define
the constructor’s body, use the parameters to initialize the object.
Copy Constructor
A copy constructor is an overloaded constructor in which an object of the
same class is passed as reference parameter
class Student
{
public:
int rollno;
string name;
// Parramertise constructor
Student(int x)
{
rollno = x;
name = "None";
}
// Parramertise constructor
Student(int x, string str)
{
rollno = x;
name = str;
}
Student()
{
rollno=20;
}
};
int main()
{
Student obj;
cout<<"Roll No="<<obj.rollno
Student A(16);
Student B(11, "Amit");
return 0;
}
3.Define a class Ele_Bill in C++ with the following descriptions:
Private members:
Cname of type character array
Pnumber of type long
No_of_units of type integer
Amount of type float.
Calc_Amount( ) This member function should calculate the
amount asNo_of_units*Cost .
Amount can be calculated accordingto the following conditions:
No_of_units Cost
First 50 units Free
Next 100 units 0.80 @ unit
Next 200 units 1.00 @ unit
Remaining units 1.20 @ unit
Public members:
* A function Accept( ) which allows user to enter Cname,
Pnumber, No_of_units and invoke function Calc_Amount().
* A function Display( ) to display the values of all the data members
on the screen.
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
class Ele_Bill
{
char Cname[30];
long Pnumber;
int No_of_units;
float Amount;
void Calc_Amount( );
public:
void Accept();
void Display();
};
void Ele_Bill : : Calc_Amount( )
{
if(No_of_units<=50)
{
Amount=0;
}
else if(No_of_units<=150)
{
Amount=(No_of_units-50)*0.80;
}
else if(No_of_units<=350)
{
Amount=80+(No_of_units-150)*1.00;
}
else
{
Amount=80+200+(No_of_units-350)*1.20;
}
}
void Ele_Bill :: Accept( )
{
gets(Cname);
cin>Pnumber>>No_of_units;
Calc_Amount( );
}
void Ele_Bill :: Display( )
{
cout<<"\nCustomer Name : "<<Cname<<"\n Phone Number : "<<Pnumber<<" \nNumber of Units : "<<No_of_units<<"\nTotal Amount : "<<Amount;
}
int main()
{
clrscr();
Ele_Bill obj;
obj.Accept();
obj.display();
getch();
return 0;
}
A copy constructor is an overloaded constructor in which an object of the
same class is passed as reference parameter
#include<iostream>
class Point
{
private:
int x, y;
public:
Point(int x1, int y1)
{ x = x1; y = y1; }
// Copy constructor
Point(const Point &p2)
{x = p2.x; y = p2.y; }
int getX()
{ return x; }
int getY()
{ return y; }
};
int main()
{
Point p1(5, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
2.What is constructor ? Give the example
Constructors are special class functions which performs initialization of every object.The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object.
Types of Constructors in C++
Constructors are of three types:
1.Default Constructor
Default constructor is the constructor which doesn't take any argument. It has no parameter.
2.Parametrized Constructor
It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created.
To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define
the constructor’s body, use the parameters to initialize the object.
Copy Constructor
A copy constructor is an overloaded constructor in which an object of the
same class is passed as reference parameter
class Student
{
public:
int rollno;
string name;
// Parramertise constructor
Student(int x)
{
rollno = x;
name = "None";
}
// Parramertise constructor
Student(int x, string str)
{
rollno = x;
name = str;
}
Student()
{
rollno=20;
}
};
int main()
{
Student obj;
cout<<"Roll No="<<obj.rollno
Student A(16);
Student B(11, "Amit");
return 0;
}
3.Define a class Ele_Bill in C++ with the following descriptions:
Private members:
Cname of type character array
Pnumber of type long
No_of_units of type integer
Amount of type float.
Calc_Amount( ) This member function should calculate the
amount asNo_of_units*Cost .
Amount can be calculated accordingto the following conditions:
No_of_units Cost
First 50 units Free
Next 100 units 0.80 @ unit
Next 200 units 1.00 @ unit
Remaining units 1.20 @ unit
Public members:
* A function Accept( ) which allows user to enter Cname,
Pnumber, No_of_units and invoke function Calc_Amount().
* A function Display( ) to display the values of all the data members
on the screen.
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
class Ele_Bill
{
char Cname[30];
long Pnumber;
int No_of_units;
float Amount;
void Calc_Amount( );
public:
void Accept();
void Display();
};
void Ele_Bill : : Calc_Amount( )
{
if(No_of_units<=50)
{
Amount=0;
}
else if(No_of_units<=150)
{
Amount=(No_of_units-50)*0.80;
}
else if(No_of_units<=350)
{
Amount=80+(No_of_units-150)*1.00;
}
else
{
Amount=80+200+(No_of_units-350)*1.20;
}
}
void Ele_Bill :: Accept( )
{
gets(Cname);
cin>Pnumber>>No_of_units;
Calc_Amount( );
}
void Ele_Bill :: Display( )
{
cout<<"\nCustomer Name : "<<Cname<<"\n Phone Number : "<<Pnumber<<" \nNumber of Units : "<<No_of_units<<"\nTotal Amount : "<<Amount;
}
int main()
{
clrscr();
Ele_Bill obj;
obj.Accept();
obj.display();
getch();
return 0;
}
No comments:
Post a Comment