21
OBJECT ORIENTED PROGRAMMING EXERCISE 9 CODING MUHAMAD ZULHAIRI BIN AB SATAR D20101038665

Opp compile

Embed Size (px)

Citation preview

Page 1: Opp compile

OBJECT ORIENTED PROGRAMMING

EXERCISE 9 CODING

MUHAMAD ZULHAIRI BIN AB SATAR

D20101038665

Page 2: Opp compile

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()

{

Page 3: Opp compile

// 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;

}

Page 4: Opp compile
Page 5: Opp compile

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;

}

Page 6: Opp compile

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.";

}

Page 7: Opp compile
Page 8: Opp compile

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();

}

Page 9: Opp compile
Page 10: Opp compile

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();

}

Page 11: Opp compile
Page 12: Opp compile

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;

Page 13: Opp compile

cout<<"The bird name is : "<<name<<endl;

}

void main()

{

bird bird1;

bird bird2("peach","peguin");

bird1.display();

bird2.display();

}

Page 14: Opp compile

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)

Page 15: Opp compile

{

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();

}

Page 16: Opp compile

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;

Page 17: Opp compile

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();

}

Page 18: Opp compile

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()

{

Page 19: Opp compile

BOX BOX1;

}

Page 20: Opp compile

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;

Page 21: Opp compile

obj1.display();

return 0;

}