Wednesday, December 17, 2014

Friend Function and Hierarchical Inheritance in C++ pro



Friend Function
         
          #include
          #include
          class two;
          class one
          {
          int a,b,c;
          public:
          void set(int x,int y, int z)
          {
          a=x;
          b=y;
          c=z;
          }
          friend void displayadd(one,two)
};
class two
{
public:
int e,f,g;
void set(int x,int y,int z)
{
e=x;
f=y;
g=z;
}
friend void displayadd(one,two)
};
void displayadd(one s1,two s2)
{
cout<<"\n Total of s1:\t"<
cout<<"\n Total of s2:\t"<
}
void main()
{
clrscr();
one s1;
two s2;
s1.set(13,12,11);
s2.set(24,23,22);
displayadd(s1,s2);
getch();
}

Output
         
          Total of s1  :         36
          Total of s2  :         69

Hierarchical Inheritance
         
          #include
          #include
          class person
          {
          private:
          char name[20], address[25];
          int age;
          public:
void get_pdata()
{
          cout<<"\n Enter the name       : ";
          cin>>name;
          cout<<"\n Enter the address    : ";
          cin>>address;
          cout<<"\n Enter the age           : ";
          cin>>age;
          void put_pdata()
          {
          cout<<"\n Name            : "<
          cout<<"\n Address         : "<
          cout<<"\n Age               : "<
          };
          class student: private person
          {
private:
int rollno;
float marks;
public:
void get_stdata()
{
get_pdata();
cout<<"\n Enter the rollnumber:";
cin>>rollno;
cout<<"\n Enter the marks: ";
cin>>marks;
}
void put_stdata()
{
cout<<"\n Roll number: "<
cout<<"\n Marks  : "<
}
};
class faculty: public person
{
char dept[20];
float salary;
public:
void get_fdata()
{
get_pdata();
cout<<"\n Enter department: ";
cin>>dept;
cout<<"\n Enter Salary: ";
cin>>salary;
}
void put_fdata()
{
put_pdata();
cout<<"\n Department : "<
cout<<"\n Salary  : "<
}
};
void main()
{
student stobj;
faculty facobj;
clrscr();
cout<<"\n Enter the details of the student\n";
cout<<"\n-----------------------------------------------------";
stobj.get_stdata();
cout<<"\n Enter the details of the faculty member";
cout<<"\n -----------------------------------------------------";
facobj.get_fdata();
cout<<"\n\n\n Student info is";
cout<<"\n-----------------------------------------------------";
stobj.put_stdata();
cout<<"\n\n\n Faculty member info is";
cout<<"\n--------------------------------------------------";
facobj.put_fdata();
getch();
}

Output
       
         Enter the details of the student
          -------------------------------------------------------------------
          Enter the name               :         Raja
          Enter the address           :         Chennai
          Enter the age                  :         15
          Enter the roll number     :         26
Enter the mark               :         95
Enter the details of the faculty member
---------------------------------------------------------------------
Enter the name               :         Ragul
Enter the address           :         Mumbai     
Enter the age                  :         30     
Enter the department     :         Mathematics
Enter the salary             :         12000
                             Student info is
                   -------------------------------------------
                   Name                    :         Raja
                   Address                :         Chennai
                   Age                       :         15
                   Roll number                   :         26
                   Mark                    :         95
                             Faculty member info is
                   ----------------------------------------
                   Name                    :         Ragul
                   Address                :         Mumbai
                   Age                       :         30
                   Department          :         Mathematics
                   Salary                            :         12000

No comments:

Post a Comment