Subscribe Us

OOP in C++ (Object Oriented Programming) - Coding in C++

 OOP in C++ (Object Oriented Programming). Here you will find OOP programs that are very important and very easy.

Program 1:

 

#include<iostream>

using namespace std;

class Test                      //create class by using name ‘Test’ along with class keyword

{  

    private: 

     int x;                         //private data member       

    float y;                      //private data member

    protected:

    public:   

      int z;                        //public data member

    void setX()                 //member function to set data                       

     {            

       cin>>x;                    //assigning value to 'x'                  

      } 

      void setY()                        //member function to set data                        

      {           

       cin>>y;                   //assigning value to 'y'         

      } 

      void getX()                       //member function to get data              

     {            

      cout<<x;                           //displaying value of 'x'                 

     }  

      void getY()                       //member function to get data                       

     {            

       cout<<y;                         //displaying value of 'y'          

     } 

};

int main()                                //main function

{

Test obj;                                  //object declaration

obj.x=10;                                 //private data member

obj.y=20;                                 //private data member

obj.z=30;                                 //public data member

obj.setX();                               //public data member

obj.setY();                                //public member function

obj.getX();                               //public member function

obj.getY();                               //public member function

cout<<obj.z;                           //public data member

return 0;

}

 

Program 2:

 

#include <iostream>

using namespace std;

class Box                                       //create class by using name ‘Box’ along with class keyword

{

    private:

            int length;                        // Length of a box

            int breadth;                     // Breadth of a box

   public:

    int height;                                // Height of a box

    void setData(int l, int b,int h)   //member function to set data

                    {

                        length=l;

                        breadth=b;

                        height=h;

                    }

            int getLength()                    //member function to get data

                    {return length;}

            int getBreadth()                  //member function to get data

                    {return breadth;}

};

 

int main( )                                 //main function

 {

     Box b1;

     //setting length, breadth and height

     b1.setData(10,6,5);

  

    //accessing private members using member functions  

   cout<<"Length of Box: "<<b1.getLength()<<endl;

   cout<<"Breadth of Box: "<<b1.getBreadth()<<endl;

  

   //accessing public member height using (.) operator

   cout<<"Height of Box: "<<b1.height<<endl;

   return 0;

 }

 

Program 3:

Write a program that will display the result card of a student: a result card will contain marks of five subjects and display the CGPA of the student in the first semester.

 

 

#include <iostream>

using namespace std;

 

class student                                  //create class by using name ‘student’ along with class keyword                                       

{                                            

          private:                                //private access specifier

          int l;                                       //private data member                             

          public:                                  //public access specifier

          void calculateCGPA()         //member function to calculate CGPA

          {

          cout<<"-------------- CGPA Calculating -----------------"<<endl;

          cout<<"How many semester results do you want input :";

          cin>>l;                                    //assigning value to ‘l’

          cout<<endl;

          float sems[l];                        

          for(int i=1;i<=l;i++)               //using for loop

    {

        cout<<" Enter Semester "<<i<<" Result(GPA): ";

        cin>>sems[i];

        cout<<endl;

    }

    float sem_total=0;

    for(int j=1;j<=l;j++)                   //using for loop

    {

        sem_total=sem_total+sems[j];

    }

 

    cout<<"*** Your CGPA is "<<sem_total/l<<" *****"<<endl;

          }

};                         //we must use semicolon at the end of class.

int main()          //main function

{

          student result;

          result.calculateCGPA();

         

          return 0;

}

 

 

Program 4:

 

#include <iostream>

using namespace std;

class Student                         //create class by using name ‘student’ along with class keyword

{

private:                          //private access specifier

char name[10];            //private data members

int roll_No;                   //private data members

int m1, m2, m3;           //private data members

int total, average;    //private data members

protected:                    //protected access specifier

public:                           //public access specifier

void set_Data()            //member function to set data

{

cout<<"Enter student name::";             

cin>>name;

cout<<"Enter roll no of the student::"; 

cin>>roll_No;

cout<<"Enter marks of first subject::"; 

cin>>m1;

cout<<"Enter marks of  second subject::";    

cin>>m2;

cout<<"Enter marks of third subject::";         

cin>>m3;

 }

void get_Average()        //member function to get data

{

cout<<"student name::”<<  name <<endl;

cout<<"roll no::"<<  roll_No<<endl;

cout<<"marks of first subject::” <<  m1<<endl;

cout<<"marks of second subject::  "<<  m2<<endl;

 cout<<"marks of third subject::   "<<  m3<<endl;

 

total=m1+m2+m3;

average=total/3;

 cout<< "Average marks of " <<name<< " will be "<<average;

}

};                           //we must use semicolon at the end of class.

int main()            //main function

{

Student S1, S2, S3;

 S1.set_Data();

S1.get_Average();

 

S2.set_Data();

S2.get_Average();

 

S3.set_Data();

S3.get_Average();

}

 

Program 5 :

Write a program that declares a class with one integer data member and two member function in() and out() to input and output data in the data member.

 

#include <iostream>

using namespace std;

class Test                    //create class by using name ‘Test’ along with class keyword

{

          private:                         //private access specifier

          int n;                              //private data member

          public:                           //public access specifier

          void in()                         //member function to input data

          {

                   cout<<"Enter a number:";

                   cin>>n;

          }

          void out()                      //member function to receive or display data

          {

                   Cout<<"The value of n = "<<n;

          }

};                       //we must use semicolon at the end of class.

int main()        //main function

{

          Test obj;           //object declaration

          obj.in();

          obj.out();

          return 0;

}

 

Program 6:

Write a class Marks with three data members to store three marks. Write three member functions in() to input marks, sum() to calculate and return the sum, avg() to calculate and return the average marks.

 

#include <iostream>

using namespace std;

class Marks                              //create class by using name ‘Marks’ along with class keyword

{

          private:                         //private access specifier

          int a,b,c;                        //private data members

          public:                           //public access specifier

          void in()                        //member function to input data

          {

                   cout<<"Enter three marks: ";

                   cin>>a>>b>>c;

          }

          int sum()                         //member function to get sum

          {

                   return a+b+c;

          }

          float avg()                      //member function to get avg

          {

                   return (a+b+c)/3;

          }

};                     //we must use semicolon at the end of class.

int main()      //main function

{

          Marks m;             //object declaration

          int s;

          float a;

          m.in();

          s = m.sum();

          a = m.avg();

          cout<<"Sum = "<<s<<endl;

          cout<<"Average = "<<a;

          return 0;

}

 

Program 7:

Write a class circle with one data member radius. Write three member function get_radius() to set radius value with parameter value, area() to display radius and circum() to calculate and display the circumference of circle.

 

 

#include <iostream>

using namespace std;

class circle                                      //create class by using name ‘circle’ along with class keyword

{

          private:                                //private access specifier

          float radius;                        //private data member

          public:                                 //public access specifier

          void get_radius(float r)    //member function to get radius

          {

                   radius = r;

          }

          void area()             //member function to calculate area

          {

                   cout<<"Area of circle "<<3.14*radius*radius<<endl;

          }

          void circum()        //member function to calculate circumference

          {

                   cout<<"Circumference of circle "<<2*3.14*radius;

          }

};                              //we must use semicolon at the end of class.

int main()               //main function

{

          circle c;        //object declaration

          float rad;

          cout<<"Enter radius ";

          cin>>rad;

          c.get_radius(rad);

          c.area();

          c.circum();

          return 0;

}

 

Program 8:

 

#include <iostream>

using namespace std;

class Book                                 //create class by using name ‘Book’ along with class keyword

{

          private:                          //private access specifier

          int BookID, Pages;        //private data member

          float Price;                     //private data member

          public:                            //public access specifier

          void get()                       //member function to get data

          {

                   cout<<"Enter Book ID:";

                   cin>>BookID;

                   cout<<"Enter Pages:";

                   cin>>Pages;

                   cout<<"Enter price:";

                   cin>>Price;

          }

          void show()                      //member function to show data

          {

                   cout<<"BookID = "<<BookID<<endl;

                   cout<<"Pages = "<<Pages<<endl;

                   cout<<"Price = "<<Price<<endl;

          }

          void set(int id,int pg, float pr)       //member function to set data

          {

                   BookID = id;

                   Pages = pg;

                   Price = pr;

          }

          float getPrice()                                 //member function to get price

          {

                   return Price;

          }

};                          //we must use semicolon at the end of class.

int main()           //main function

{

          Book b1, b2;        //object declaration

          b1.get();

          b2.set(2, 320, 150.75);

          cout<<"\nThe detail of most costly book is as follows:"<<endl;

          if(b1.getPrice()>b2.getPrice())

          b1.show();

          else

          b2.show();

          return 0;

}

 

Program 9:

 

 

#include <iostream>

using namespace std;

class Result                               //create class by using name ‘Result’ along with class keyword

{

          private:                          //private access specifier

          int rno, marks[3];         //private data member

          char name[50];             //private data member

          public:                             //public access specifier

          void input()                    //member function to input data

          {

                   cout<<"Enter Roll No:";

                   cin>>rno;

                   cout<<"Enter name:";

                   gets(name);

                   for(int i=0; i<3; i++)

                   {

                             cout<<"Enter marks ["<<i<<"]:";

                             cin>>marks[i];

                   }

          }

          void show()                          //member function to show data

          {

                   cout<<"Roll No = "<<rno<<endl;

                   cout<<"Name = "<<name<<endl;

                   for(int i=0; i<3; i++)

                   cout<<"Marks["<<i<<"]:"<<marks[i]<<endl;

          }

          int total()                        //member function to get total

          {

                   int t=0;

                   for(int i=0; i<3; i++)

                   t=t+marks[i];

                   return t;

          }

          float avg()                            //member function to calculate avg

          {

                   int t=0;

                   for(int i=0; i<3; i++)

                   t=t+marks[i];

                   return t/3.0;

          }

};                         //we must use semicolon at the end of class.

int main()

{

          Result r;

          r.input();

          r.show();

          cout<<"\nTotal marks ="<<r.total()<<endl;

          cout<<"Average marks ="<<r.avg()<<endl;

          return 0;

}

 

Program 10: 

 

#include <iostream>

using namespace std;

class student                    //create class by using name ‘student’ along with class keyword

                            

{                                                                          

          private:                    //private access specifier                             

          char name[20];       //private data member             

          int ad_no;               //private data member    

          float eng,math,science;        //private data member

          float total;                    //private data member

                  

          float ctotal()                 //a function to calculate eng + math + science with float return type.

                  

        {

          return eng+math+science;

        }

          protected:                              //private access specifier

          public:                                    //public access specifier

 

 

          void Takedata()                   //member function to Take data

          {                                              

                   cout<<"Enter student name: "; cin>>sname;                    

                   cout<<"how many number you want to enter: "

                    cin>>ad_no;                                                       

                   cout<<"Enter marks of English: "; cin>>eng;                    

                   cout<<"Enter marks of  Mathematics: "; cin>>math;                

                   cout<<"Enter marks of Science: "; cin>>science;                       

                   total = ctotal();                                                                               

          }                                    

 

 

          void Showdata()                       //member function to Show data

          {                                              

                   cout<<"Student name: "<< sname <<endl;                                         

                   cout<<"The number you enter is : "<< ad_no <<endl;                                                             

                   cout<<"Marks of English: "<< eng <<endl;                                 

                   cout<<"Marks of Mathematics: "<< math <<endl;                    

                   cout<<"Marks of Science: "<< science <<endl;                

                   cout<<"Total marks: "<<total;                                                               

          }                                              

};                                        //we must use semicolon at the end of class.              

 

int main()                    //main function

{

          student obj;      //object declaration

          obj.Takedata();

          obj.Showdata();

         

          return 0;

}

 

Program 11:

 

 

#include <iostream>

using namespace std;

class batsman                          //create class by using name ‘batsman’ along with class keyword

{

private:                     //private access specifier

string name;

int b_code;

int innings,notout,runs;

float bat_avg;

float cal_avg()

                   {

                             return runs/(innings-notout);

                   }

                  

public:                                  //public access specifier

void set_data()                   //member function to set data

                   {

                             cout<<"Enter the name of batsman : "<<endl;

                             cin>>name;

                             cout<<"Enter the code of batsman : "<<endl;

                             cin>>b_code;

                             cout<<"Enter total innings of batsman : "<<endl;

                             cin>>innings;

                             cout<<"Enter total runs of batsman : "<<endl;

                             cin>>runs;

                             cout<<"Enter numbers of Notout of batsman : "<<endl;

                             cin>>notout;

                             bat_avg=cal_avg();

                             cout<<"__________________"<<endl;

                            

                   }

                   void get_data()                //member function to get data

                   {

                             cout<<"Name of batsman : "<<name<<endl;

                             cout<<"Code of batsman : "<<b_code<<endl;

                             cout<<"Total innings   : "<<innings<<endl;

                             cout<<"Total runs      : "<<runs<<endl;

                             cout<<"Notout          : "<<notout<<endl;

                             cout<<"Average of Batsman is : "<<bat_avg<<endl;

                             cout<<"--------------------------------------------------"<<endl;

                   }

};                                     //we must use semicolon at the end of class.

 

int main()                     //main function

{

          batsman obj;           //object declaration

         

          obj.set_data();

          obj.get_data();

         

          return 0;

}

Post a Comment

0 Comments