Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Opp compile

119

Published on

1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• http://www.sendspace.com/file/8kn03w

Are you sure you want to  Yes  No
• Be the first to like this

Views
Total Views
119
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
1
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. OBJECT ORIENTED PROGRAMMING EXERCISE 9 CODINGMUHAMAD ZULHAIRI BIN AB SATAR D20101038665
• 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 rectangleint 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;}
• 4. 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;}
• 5. 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.";}
• 6. 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();}
• 7. 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();}
• 8. 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;
• 9. cout<<"The bird name is : "<<name<<endl;}void main(){ bird bird1; bird bird2("peach","peguin"); bird1.display(); bird2.display();}
• 10. 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)
• 11. { 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 objectbird 2 bird1.display(); bird2.display();}
• 12. 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;
• 13. 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();}
• 14. 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(){
• 15. BOX BOX1;}
• 16. 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;
• 17. obj1.display(); return 0;}