Tuesday 24 February 2015

Object and Classes

There are three types of access specifiers
(i)               Private
(ii)              Public
(iii)             Protected

Member function:

#include<iostream.h>
#include<conio.h>
class circle      //class name
{
private:     //access specifier

int radius;
float area,circum;

public:   //access specifier

void get_radius()      //member function
{
cout<<”Enter the radius:”;
cin>>radius;
}
void calculate()        //member function
{
area=3.14*radius*radius;
circum=2*3.14*radius;
}
void show_result()        //member function
{
cout<<”Area:”<<area<<endl<<”Circumference:”<< circum<<endl;
}
};
void main()
{
circle c1;     //object
clrscr();
c1.get_radius();
c1.calculate();
c1.show_result();
getch();
}

Member function using outside of the class

: : à Scope Resolution Operator

#include<iostream.h>
#include<conio.h>
class circle      //class name
{
private:     //access specifier
int radius;
float area,circum;
public:   //access specifier
void get_radius()      //member function
void calculate()        //member function
void show_result()        //member function
};
void circle::get_radius()   //class name & member
function scope resolution operator will come
{
cout<<”Enter the radius:”;
cin>>radius;
}
void circle::calculate()      //class name member
function scope resolution operator will come
{
area=3.14*radius*radius;
circum=2*3.14*radius;
}
void circle::show_result()     //class name member
 function scope resolution operator will come
{
cout<<”Area:”<<area<<endl<<”Circumference:”<< circum<<endl;
}
void main()
{
circle c1;     //object
clrscr();
c1.get_radius();
c1.calculate();
c1.show_result();
getch();
}

Member function overloading:

#include<iostream.h>
#include<conio.h>
class rectangle   //class name
{
private:   //access specifier
int length, breadth, area;
public:      //access specifier
void getdata()    //member function
{
cout<<”Length value:”;
cin>>length;
cout<<”Breadth value:”;
cin>>breadth;
area=breadth*length;
}
void getdata(int a)   //member function overloading
{
length=a;
cout<<”Breadth value:”;
cin>>breadth;
area=breadth*length;
}
void showdata()     //member function
{
cout<<”Length:”<<length<<”Breadth:”<<breadth<<”The area is:”<<area<<endl;
}
};
void main()
{
rectangle r1,r2;   //object
clrscr();
cout<<”Enter the detail for first rectangle”<<endl;
r1.getdata();
r1.showdata();
cout<<”Enter the detail for second rectangle”<<endl;
r2.getdata(3);
r2.showdata();
getch();
}


Returning values from the member function:

#include<iostream.h>
#include<conio.h>
class rectangle    //class name
{
private:     //access specifier
int length,breadth;
public:      //access specifier
void setdata(int a,int b)//member functionoverloading
{
length=a;
breadth=b;
}
int calculate()   //returning values
{
return breadth * length;
}
};
void main()
{
rectangle r;     //object
int area;
clrscr();
r.setdata(5,8);
area=r.calculate();
cout<<”Area is:”<<area;
getch();
}


Static data members and functions:

#include<iostream.h>
#include<conio.h>
class circle      //class name
{
private:     //access specifier
int radius;
float area,circum;
public:   //access specifier
void get_radius()      //member function
{
cout<<”Enter the radius:”;
cin>>radius;
}
void calculate()        //member function
{
area=3.14*radius*radius;
circum=2*3.14*radius;
}
void show_result()        //member function
{
cout<<”Area:”<<area<<endl<<”Circumference:”<< circum<<endl;
}
};
void main()
{
circle c[5];     //object
int i;
clrscr();
for(i=0;i<5;i++)
{
c[i].get_radius();
c[i].calculate();
}
for(i=0;i<5;i++)
{
cout<<”Circle:”<<i+1;
c[i].show_result();
}
getch();
}

Constructor:

#include<iostream.h>
#include<conio.h>
class exam           //class name
{
private:          //access specifier
int sno,mark1,mark2;
public:
exam()        //constructor
{
sno=mark1=mark2=0;
}
void show_data()    //member function
{
cout<<”Sno:”<<sno<<”Mark1:”<<mark1<<”Mark2:”<<mark2<<endl;
}
void getdata()           //member function
{
cout<<”Enter sno:”;
cin>>sno;
cout<<”Enter two marks:”;
cin>>mark1>>mark2;
}
};
void main()
{
exam e;   //object
clrscr();
e.showdata();
e.getdata();
e.showdata();
getch();
}

Overloaded Constructor:

#include<iostream.h>
#include<conio.h>
class exam        //class name
{
private:        //access specifier
int sno,mark1,mark2;
public:         //access specifier
exam()        //constructor
{
sno=mark1=mark2=0;
}
exam(int a, int b, int c)     //overloading constructor
{
sno=a;
mark1=b;
mark2=c;
}
void showdata()          //member function
{
cout<<”Sno:”<<sno<<endl<<”Mark1:”<<mark1<< endl<<”Mark2:”<<mark2<<endl;
}
};
void main()
{
exam e1,e2(10,45,85);     //object
clrscr();
e1.showdata();
e2.showdata();
getch();
}


Copy Constructor:

#include<iostream.h>
#include<conio.h>
#include<string.h>
class item      //class name
{
private:     //access specifier
int itno;
char itname[10];
float price;
public:      //access specifier
item(int a,char *b,float c)     //constructor overloading
{
itno=a;
strcpy(itname,b);
price=c;
}
item(item &ptr)        //copy constructor
{
itno=ptr.itno;
strcpy(itname,ptr.itname);
price=ptr.price;
}
void showitem()       //member function
{
cout<<”Item no:”<<itno<<”Item name:”<<itname<<”Price:”<<price<<endl;
}
};
void main()
{
item i1(1,’monitor’,3800),i2(i1);   //object
clrscr();
cout<<”First Item”<<endl;
i1.showitem();
cout<<”Second item:”<<endl;
i2.showitem();
getch();
}

Destructor:

~ à (Tilde) Destructor

#include<iostream.h>
#include<conio.h>
#include<string.h>
class book        //class name
{
private:        //access specifier
int book_no;
char book_name[20];
public:        //access specifier
book()       //constructor
{
book_no=10;
strcpy(book_name,”C++”);
}
~book()       //destructor
{
book_no=0;
strcpy(book_name,”  ”);
cout<<”Destructor Invoked”;
showdetail();
}
void showdetail()        //member function
{
cout<<”Book no:”<<book_no<< endl<< ”Book_name:” <<book_name<<endl;
}
};
void main()
{
book b;         //object
clrscr();
b.showdetail();
getch();
}

Friend Function:
Relating between two member function

#include<iostream.h>
#include<conio.h>
class book    //class name
{
private:        //access specifier
int book_no;
char book_name[20];
public:       //access specifier
void getdata();   //member function
friend void showdata(book);     //friend function
};
void book::getdata()   //class name & member
function scope resolution operator will come
{
cout<<”Enter the book number:”;
cin>>book_no;
cout<<”Enter the book_name:”;
cin>>book_name;
}
void showdata (book bk)  //class name & member
function scope resolution operator will come
              friend function
{
cout<<”Book no:”<<bk.book_no<<”Book Name:”<<bk.book_name<<endl;
}
void main()
{
book b;         //object
clrscr();
b.getdata();
showdata(b);
getch();
}

Friend Class:
Relating between two class

#include<iostream.h>
#include<conio.h>
class first         //class name
{
private:        //access specifier
int first_num;
public:           //access specifier
friend class second;   //second declared as a friend
                                                of first
first(int i)           //constructor overloading
{
first_num=i;
}
};
class second     // class name
{
public:           //access specifier
void showdata(first f)//member function overloading
{
cout<<”The value is:”<<f.first_num;
}
};
void main()
{
first f(10);      //object
second s;          //object
clrscr();
s.showdata(f);
getch();

}

No comments:

Post a Comment