opp compile
TRANSCRIPT
OBJECT ORIENTED PROGRAMMING
EXERCISE 9 CODING
MUHAMAD ZULHAIRI BIN AB SATAR
D20101038665
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()
{
// 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<<" \n\n ";
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;
}
EXERCISE 2
#include<iostream.h>
class coordinate
{
private:
int x,y;
public:
void setX(int theX);
void findY();
void printXY(void);
};
void coordinate::printXY(void)
{
cout<<"The coordinate is ("<<x<<","<<y<<")";
}
void coordinate::findY()
{
y=(2*x)+1;
}
void coordinate::setX(int theX)
{
x=theX;
}
void main(void)
{
coordinate point;
// first coordinate of (x,y)
int userX;
cout<<"LINEAR GRAPH: y=2x+1"<<endl;
cout<<"Enter the X point?";
cin>>userX;
//set and display coordinate (x,y)
point.setX(userX);
point.findY();
point.printXY();
cout<<endl<<endl<<"End of program.";
}
EXERCISE 3
#include<iostream.h>
class bird{
private :
char *color;
char *name;
public :
bird();
void display();
};
bird::bird(){
color="black";
name="jackjaw";
}
void bird::display(){
cout<<"The bird color is"<<color<<endl;
cout<<"The bird name is"<<name<<endl;
}
void main(){
bird bird1;
bird1.display();
}
EXERCISE 4
#include<iostream.h>
class bird{
private :
char *color;
char *name;
public :
bird(char *c, char *n);
void display();
};
bird::bird(char *a, char *n){
color=a;
name=n;
}
void bird::display(){
cout<<"The bird color is : "<<color<<endl;
cout<<"The bird name is : "<<name<<endl;
}
void main()
{
bird bird1("pink","Peacock");
bird1.display();
}
EXERCISE 5
#include<iostream>
class bird{
private :
char *color;
char *name;
public :
bird();
bird(char *c, char *n);
void display();
};
bird::bird()
{
color="Purple";
name="Peacock";
}
bird::bird(char *a, char *n){
color=a;
name=n;
}
void bird::display()
{
cout<<"The bird color is : "<<color<<endl;
cout<<"The bird name is : "<<name<<endl;
}
void main()
{
bird bird1;
bird bird2("peach","peguin");
bird1.display();
bird2.display();
}
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)
{
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();
}
EXERCISE 7
#include<iostream>
using namespace std;
class student{
char *hair_color;
char *skin_color;
public:
student();
student(char*hc,char*sc);
student(student &std);
void display();
};
student::student()
{
}
student::student(char*hcl, char*sck)
{
hair_color=hcl;
skin_color=sck;
}
student::student(student &h){
hair_color=h.hair_color;
skin_color=h.skin_color;
}
void student::display(void){
cout<<"The hair color is"<<hair_color<<endl;
cout<<"The skin color is"<<skin_color<<endl;
}
void main(){
student student0();
student student1("Brown","White");
student student2(student1);
student1.display();
student2.display();
}
EXERCISE 8
#include<iostream>
class BOX
{
int length;
int width;
public:
BOX();
~BOX();
};
BOX::BOX()
{
cout<<"Welcome To Constructor Box" <<endl;
}
BOX::~BOX()
{
cout<<"Welcome To Destructor Box"<<endl;
}
void main()
{
BOX BOX1;
}
EXERCISE 9
#include<iostream.h>
class SegiEmpat
{ private:
int i;
public:
void display (); // method declaration
SegiEmpat ();// constructor declaration
~SegiEmpat (); // destructor declaration
};
void SegiEmpat::display()
{
cout<<"\nIn function display";
}
SegiEmpat::SegiEmpat()
{
cout<<"\nIn constructor";
}
SegiEmpat::~SegiEmpat()
{
cout<<"\nIn destructor"<<endl;
}
int main()
{ SegiEmpat obj1;
obj1.display();
return 0;
}