Sunday, February 27, 2011

Inheritance

#include<iostream.h>
#include<conio.h>
 #include<stdio.h>
 class common
 {
 int rl,ph;
 char na[30];
 public:
 void getdata()
 {
 cout<<"\nEnter the name ";
 gets(na);
 cout<<"\nEnter the ID";
 cin>>rl;
 cout<<"\nEnter the phone no ";
 cin>>ph;
 }
 void showdata()
 {
 cout<<"\nThe details are=\n"<<na<<"\n"<<rl<<"\n"<<ph;
 }
 };

 class education
 {
 char uni[20];
 char sess[10];
 char res[10];
 public:
 void getmarks()
 {
 }

 void showmarks()
 {
 }

 };
 class student: public common, public education
 {
 int m;
 public:
 void getderiveddata()
 {
 common::getdata();
 cout<<"\n Enter marks=";
 cin>>m;
 }
 void showderiveddata()
 {
 common::showdata();
 cout<<m;
 }
 };
 class test:public student
 {
 };
 class employee: public common
 {
 int sal;
 public:
 void getderivedd1data()
 {
 common::getdata();
 cout<<"\n Enter salary=";
 cin>>sal;
 }
 void showderivedd1data()
 {
 common::showdata();
 cout<<sal;
 }
 };
 void main()
 {
 student s;
 s.getderiveddata();
 s.showderiveddata();
 employee e;
 e.getderivedd1data();
 e.showderivedd1data();
 getch();
 }
 

Function Overloading using Classes

#include<iostream.h>
#include<conio.h>
 class demo
 {
 public:
 void fun()
 {
 cout<<"\n This is a demo 1";
 }

void fun(char *i)
 {
 cout<<"\n This is a demo "<<i;
 }
 };

void main()
 {
 clrscr();
 demo obj;
 obj.fun();
 obj.fun("Hello ");
 getch();
 }

Function Overloading

#include<iostream.h>
#include<conio.h>
 void fun()
 {
 cout<<"\n Function 1";
 }

 void fun(int i)
 {
 cout<<"\n Function 2";
 }
 void fun(char c)
 {
 cout<<"\n Function 3";
 }
 void main()
 {
 clrscr();
 fun();
 fun(10);
 fun('w');
 }



Friend Function

#include<iostream.h>
#include<conio.h>
 class first;
 class second
 {
 int a;
 public:
 second()
 {
 a=3;
 }
 friend void sum(first , second );
 };

 class first
 {
 int b;
 public:
 first()
 {
 b=10;
 }
 friend void sum(first ,second );
 };

 void sum(first fobj, second sobj)
 {
 int s;
 s= fobj.b + sobj.a;
 cout<<"\n Sum is "<<s;
 }

void main()
 {
 clrscr();
 first f;
 second s;
 sum(f,s);
 getch();
 }

File Handling reading and writing in C++

#include<fstream.h>
#include<conio.h>
 class employee
 {
 int emp_id;
 char emp_na[40];
 public:
 void get_data()
 {
 cout<<"\n enter Emp Id:";
 cin>>emp_id;
 cout<<"\n enter Emp Name:";
 cin>>emp_na;
 }

void show_data()
 {
 cout<<"\n Employee Id:"<<emp_id;
 cout<<"\n Employee Name:"<<emp_na;
 }
 };

void main()
 {
 clrscr();
 employee e1;
 ofstream fp;
 ifstream fp1;
 fp.open("emp.txt",ios::app);
 e1.get_data();
 fp.write((char*)&e1,sizeof(e1));
 fp.close();
 getch();

fp1.open("emp.txt",ios::in);
 fp1.read((char*)&e1,sizeof(e1));
 while(!fp1.eof())
 {
 e1.show_data();
 fp1.read((char*)&e1,sizeof(e1));
 }
 fp.close();
 getch();
 }

Object as an argument to function

#include<iostream.h>
#include<conio.h>
 class distance
 {
 int feet;
 float inches;
 public:
 distance()
 {
 feet= 5;
 inches= 6.7;
 }
 void getdata()
 {
 cout<<"\nEnter the feet value=";
 cin>>feet;
 cout<<"\nEnter the inches value=";
 cin>>inches;
 }
 void showdata()
 {
 cout<<"\nvalue of distance is="<<feet<<"-"<<inches;
 }
 distance adddist(distance);
 };
 distance distance::adddist(distance d3)
 {
 distance temp;
 temp.feet=feet+d3.feet;
 temp.inches=inches+d3.inches;
 return temp;
 }

void main()
 { clrscr();
 distance d1,d2,d4;
 d1.showdata();
 getch();
 d1.getdata();
 getch();
 d2.getdata();
 getch();
 d4= d1.adddist(d2);
 d4.showdata();
 getch();
 }

Operator Overloading 2

#include<iostream.h>
#include<conio.h>
 class distance
 {
 int feet;
 float inches;
 public:
 distance()
 {
 feet=0;
 inches=0.0;
 }

void getdist()
 {
 cout<<"\n Enter Feets ";
 cin>>feet;
 cout<<"\n Enter Inches ";
 cin>>inches;
 }
 void showdist()
 {
 cout<<"\n Distance is "<<feet<<"-"<<inches;
 }

 distance operator+(distance);

};

 distance distance::operator+(distance d)
 {
 distance temp;
 temp.feet=feet+d.feet;
 temp.inches=inches+d.inches;
 return temp;
 }

void main()
 {
 distance d1,d2,d3,d4,d5;
 clrscr();
 /////////////////

int a,b,c;
 a=10;
 b=20;
 c=a+b;
 cout<<"\n Sum is "<<c;

///////////////////////

d1.getdist();
 d2.getdist();
 d4.getdist();
 d5.getdist();
 d3=d1 + d2 + d4 + d5;
 d3.showdist();
 getch();
 }

Operator Overloading 1

#include<iostream.h>
#include<conio.h>
 class demo
 {
 int a;
 int b;
 int c;
 public:
 demo()
 {
 a=10;
 b=20;
 }

void show()
 {
 cout<<"\n Sum is "<<c;
 }

demo operator+(demo);
 };

demo demo::operator+(demo d)
 {
 demo temp;
 temp.a=a+d.a;
 temp.b=b+d.b;
 temp.c=temp.a+temp.b;
 return temp;
 }

 void main()
 {
 demo d1,d2,d3;
 d3=d1+d2;
 d3.show();
 getch();
 }

Virtual Class

#include<iostream.h>
#include<conio.h>
 class base
 {
 public:
 int x;
 base()
 {
       cout<<"\n Setting the value of X";
        x=10;
       getch();
 }
 };

class drived1:virtual public base
 {
    public:
    drived1()
    {
        cout<<"\ Drived 1";
        getch();
     }
 };

class drived2:virtual public base
 {
   public:
   drived2()
   {
    cout<<"\n Drived 2";
           getch();
    }
 };

class drived3:public drived1, public drived2
 {
 public:
 void show()
 {
 cout<<"\n Value of X is "<<x;
 }
 };

 void main()
 {
 clrscr();
 drived3 obj;
 obj.show();
 getch();
 }

Virtual Functions


#include
#include
class base
{
public :
virtual void show()=0;
      /* {
cout<<"\n Base Class ";
}*/
};

class drived1:public base
{
public:
void show()
{
cout<<"\n Drived 1";
}
};

class drived2:public base
{
public:
void show()
{
cout<<"\n Drived 2";
}
};

void main()
{
clrscr();
base *ptr;

// base obj;
drived1 d1;
drived2 d2;

d1.show();
// obj.show();

ptr=&d1;
ptr->show();

ptr=&d2;
ptr->show();

       getch();
}