SlideShare ist ein Scribd-Unternehmen logo
1 von 21
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<<" 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;



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

}

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
C++ programs
C++ programsC++ programs
C++ programs
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
Binomial heap
Binomial heapBinomial heap
Binomial heap
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBTDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
 
Pointers
PointersPointers
Pointers
 
P1
P1P1
P1
 
Railwaynew
RailwaynewRailwaynew
Railwaynew
 
Oopppp
OoppppOopppp
Oopppp
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 

Andere mochten auch (9)

Powerpoint "A day in the city"
Powerpoint "A day in the city"Powerpoint "A day in the city"
Powerpoint "A day in the city"
 
Doc3
Doc3Doc3
Doc3
 
The Doping Control Process
The Doping Control ProcessThe Doping Control Process
The Doping Control Process
 
Plataforma colaborativa
Plataforma colaborativaPlataforma colaborativa
Plataforma colaborativa
 
Whereabout Information Guide
Whereabout Information GuideWhereabout Information Guide
Whereabout Information Guide
 
Swamp
SwampSwamp
Swamp
 
Amy´s dream
Amy´s dreamAmy´s dream
Amy´s dream
 
Presentation Xtreme Design and Engineering
Presentation Xtreme Design and EngineeringPresentation Xtreme Design and Engineering
Presentation Xtreme Design and Engineering
 
Amy´s dream
Amy´s dreamAmy´s dream
Amy´s dream
 

Ähnlich wie Opp compile

Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfHIMANSUKUMAR12
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++Dendi Riadi
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 
c++ project on restaurant billing
c++ project on restaurant billing c++ project on restaurant billing
c++ project on restaurant billing Swakriti Rathore
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical fileMitul Patel
 

Ähnlich wie Opp compile (20)

Virtual inheritance
Virtual inheritanceVirtual inheritance
Virtual inheritance
 
Qno 1 (d)
Qno 1 (d)Qno 1 (d)
Qno 1 (d)
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
12
1212
12
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
C++ file
C++ fileC++ file
C++ file
 
c++ project on restaurant billing
c++ project on restaurant billing c++ project on restaurant billing
c++ project on restaurant billing
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
 

Opp compile

  • 1. OBJECT ORIENTED PROGRAMMING EXERCISE 9 CODING MUHAMAD 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 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; }
  • 4.
  • 5. 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; }
  • 6. 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."; }
  • 7.
  • 8. 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(); }
  • 9.
  • 10. 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(); }
  • 11.
  • 12. 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;
  • 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(); }
  • 16. 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;
  • 17. 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(); }
  • 18. 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() {
  • 20. 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;
  • 21. obj1.display(); return 0; }