2. EXERCISE 1
#include<iostream.h>
class Rectangle
{
public:
int length;
int width;
public:
void set_data(int x, int y);
double calculate_area();
void display();
};
void Rectangle::set_data(int x, int y)
{
length=x;
width=y;
}
double Rectangle::calculate_area()
{
return length*width;
}
void main()
{
3. // object declaration
Rectangle r1,r2;
//calling method set_data()
r1.set_data(10,20); //length and width for the first rectangle
int a,b;
cout<<" Enter The Second Rectangle "<<endl;
cout<<" a : ";
cin>>a;
cout<<" b : ";
cin>>b;
// calling method
r2.set_data(a,b);
// not valid because it is private attribute
r2.length=a;
cout<<" nn ";
cout<<" HERE ARE THE AREA FOR THE FIRST AND THE SECOND RECTANGLE "<<endl;
cout<<" -----------------------------------------------------------"<<endl;
cout<<"Area for first rectangle is: "<<r1.calculate_area()<<" nArea for second rectangle is:
"<<r2.calculate_area()<<endl;
}
13. cout<<"The bird name is : "<<name<<endl;
}
void main()
{
bird bird1;
bird bird2("peach","peguin");
bird1.display();
bird2.display();
}
14. EXERCISE 6
#include<iostream>
class bird
{
private :
int num_legs;
int num_wings;
public :
bird(int l, int w);
bird(bird &num);
void display();
};
bird::bird(int le, int wi)
{
num_legs=le;
num_wings=wi;
}
bird::bird(bird &br)
{
num_legs=br.num_legs;
num_wings=br.num_wings;
}
void bird::display(void)
15. {
cout<<"The number of legs are :"<<num_legs<<endl;
cout<<"The number of wings are :"<<num_wings<<endl;
}
void main()
{
bird bird1(2, 2); //object bird1 is created and initialized
bird bird2(bird1);//object bird 2 is created and the values of object bird1 are copied into object
bird 2
bird1.display();
bird2.display();
}