Tuesday, November 27, 2012

OOP Inheritance Three class and passing the arguments in it

Posted by at 3:02 AM
Object
Create a three class, Teacher,Student, and parent class Person and inherit them using the simple concept of inheritance.
Source Code
#include <iostream>
#include <string>
using namespace std;

class person
{
private:
        int age;
        char name[25];
public:

    person()
    {}
    void input(int a,char n[25])
    {
        age=a;
        strcpy(name,n);

        cout<<"Age : "<<age<<"Name : "<<name<<endl;
    }

};
////////////////////////////////////////////////////////////////

class student:public person
{
    int semester;
    int roll;
public:
    void getinput(int s,int r)
    {
    semester=s;
    roll=r;
    cout<<"Semester : "<<semester<<" Roll number : "<<roll<<endl;
    }
    /////////////////////////////////////////////////////////////
};class teacher:public person
{
    int coursecode;
    int dept;
public:
    void setinput(int s,int r)
    {
    coursecode=s;
    dept=r;
    cout<<"Course code : "<<coursecode<<" Department number : "<<dept<<endl;
    }
};
/////////////////////////////////////////////////////////////////

void main()
{
    char select;
    int ag,rol,sst,dept,ccd;
    char name[25];
    cout<<"Enter 1 for Student"<<endl;
    cout<<"Enter 2 for Teacher"<<endl;
    cin>> select;

    switch(select)
    {
    case '1':
        {
    student stdent;
    cout<<"Enter name"<<endl;
    fflush(stdin);
    cin.getline(name,25);
   
    cout<<"Enter age"<<endl;
    cin>>ag;

   

    cout<<"Enter student roll number"<<endl;
    cin>>rol;
    cout<<"Enter student semester number"<<endl;
    cin>>sst;

    stdent.input(ag,name);
    stdent.getinput(sst,rol);
    break;
        }

    case '2':
        {
    teacher teach;
    cout<<"Enter name"<<endl;
    fflush(stdin);
    cin.getline(name,25);
   
    cout<<"Enter age"<<endl;
    cin>>ag;

   
       
    cout<<"Enter Deptment number"<<endl;
    cin>>dept;

    cout<<"Course code"<<endl;
    cin>>ccd;

    teach.input(ag,name);
    teach.setinput(ccd,dept);
        break;
        }

    }
}
Output:

OOP Inheritance Simple Program

Posted by at 2:42 AM
Object:
Program to get the Area of the Rectangle by using concept of Inheritance

Source Code

#include <iostream>

using namespace std;

// Base class
class Shape
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};

 Derived class
class Rectangle: public Shape
{
   public:
      int getArea()
      {
         return (width * height);
      }
};

int main(void)
{
   Rectangle Rect;

   Rect.setWidth(5);
   Rect.setHeight(7);

   // Print the area of the object.
   cout << "Total area: " << Rect.getArea() << endl;

   return 0;
}

Output:


Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template