Social Icons

banner image

INHERITANCE IN C++

INHERITANCE IN C++

PROGRAME:-

#include<iostream>
using namespace std;
class basic
{
      private :
            char name[50];
            char reg_no[25];
            char section[14];
   public:
        void getdata();
        void displaydata();
};
void basic::getdata()
{
      cout<<"enter  a name";
      cin>> name;
      cout <<"enter reg nu";
      cin>> reg_no;
      cout <<"enter yur section";
      cin>> section;
}
void basic :: displaydata()
{
      cout <<name << "  ";
      cout << reg_no<< "  ";
      cout << section<< "  ";
     
}
int main()
{
 basic a;
 cout<<" enter the following details";
 a.getdata();
 cout << "       /n";
 cout<< "name reg_no section /n";
 cout << "      /n";
 a.displaydata();
 cout <<endl;
 cout<< "    /n";
 return 0;

}

AMBIGUITY:-

#include<iostream>
#cons int MAX =100;
using namespace std;
class baseA
{
      private:
            int i;
      public :
            void getdata();
            void display();
           
};
class baseB
{
      private:
            int j;
      public :
            void getdata();
            void display();
           
};
class derivedC: public baseA,public baseB
{
     
};
void baseA ::getdata(int x)
{
      i=x;
}
void baseA:: display()
{
      cout<<"value of i ="<<i<<  endl;
}
void baseB:: getdata(int y)
{
      j=y;
}
void baseB::display()
{
      cout << "value of j is="<<j<<endl;
}
int main()
{
      derivedC o;
      int x,y;
      cout<<"enter a value of x;";
      cin>> x;
      o.baseA::getdata(x);
      cout <<" enter a value of y";
      cin>>y;
      o.baseB::getdata(y);
      o.baseA::display();
      o.baseB::display();
      return 0;
}


ARRAY:-


#include<iostream>
using namespace std;
class basic_info
{
      private:
            char name[20];
            int roll[36];
            char sex[2];
      public:
            void getdata();
            void display();
};

class physical_fit:public basic_info
{
      private :
            float height[5];
            float weight[6];
      public :
            void getdataA();
            void displayA();
};
void physical_fit:: getdataA()
{
      basic_info::getdata();
      cout<<"enter yur height";
      cin>> height;
      cout<<"enter yur weight";
      cin>> weight;
}
void physical_fit::displayA()
{
      basic_info::display();
      cout<<height<<"      ";
      cout<<weight<< "      ";
     
}

void basic_info::getdata()
{
      cout<<"enter yur name \n";
      cin>>name;
      cout<<"enter yur roll no \n";
      cin>>roll;
      cout<<"enter yur sex \n";
      cin>>sex;
     
}
void basic_info::display()
{
      cout<<name<< "      '";
      cout<<roll <<"       ";
      cout<< sex<< "       ";
}
class acadmics::public basic_info
{
      private:
            char course;
            char sem;
            int rank;
      public:
            void getdata2();
            void display2();
           
}
void acadmics::getdata2()
{
      basic_info::getdata();
      cout<<"enter yur course";
      cin>>course;
      cout<<"enter yur sem";
      cin>> sem;
      cout<<"enter yur rnk";
      cin>>rank;
}
void acadmics::display2()
{
      basic_info::display();
      cout<<course<< "     ";
      cout<<sem<<  "        ";
      cout<<rank<< "         "
}
int main()
{     pysical_fit a1[MAX];
    int i ,n;
    cout <<"nter number of students\n";
    cin>>n

    pysical_fitness p;
    acadmics        a;
     
      cout<<"enter the following info fr physical_fitness";
      for(i=0;i=n-1;++i)
      {
            cout<<" record: "<<i+1<<endl;
            a[i].getdata1();
      }
      cout<<" name roll sex height   weight";
      cout<<"-----------------------/n";
      for(i=0;i=n-1;++i)
      {
            cout<<" record: "<<i+1<<endl;
            a[i].display();
      }
      cout<<"------------------------/n";
      cout<<endl;
      cout<<"enter the following info fr acadmics";
      cout<<" name roll sex course sem rank";
      a.getdata2();
      cout<<"-----------------------/n";
      a.display2();
      cout<<"------------------------/n";
      return 0;
}

ARRAY IN MULTIPLE INHERITANCE:-

#include<iostream>
using namespace std;
class basic_info
{
      private:
            char name[5];
            int roll [3];
            char sex[2];
      public:
            void getdata();
            void display();
};
class acadmics
{
      private:
            char course[5];
            int sem [3];
            int rank[2];
      public:
            void getdataA;
            void displayA;
};
class financial: public basic_info;public acadmics;
{
      private:
            float amount ;
      public:
            void getdataB();
            void displayB();
           
}
void basic_info:: getdata()
{
      cout <<"enter a name /n";
      cin>> name ;
            cout <<"enter a roll name /n";
      cin>> roll;
            cout <<"enter a sex /n";
      cin>> sex;

}
void student:: displaydata()
{
      cout<<name<<"       ";
      cout << roll<< "      ";
      cout<<sex<< " ";
}
void acadmics:: getdataA()
{
      cout <<"enter a course /n";
      cin>> course ;
            cout <<"enter a sem /n";
      cin>> sem;
            cout <<"enter a rank /n";
      cin>> rank;

}
void acadmics:: displayA()
{
      cout<<course<<"       ";
      cout << sem<< "      ";
      cout<<rank<< " ";
}
void financial::getdataB()
{   
    basic_info::getdata();
    acadmics::displayA();
      cout <<"enter a amount";
      cin>>amount;
}
void basic_info::getdataB()
{
      basic_info::display();
      acadmics::displayA();
      cout<<amount <<"       ";
     
}
int main()
{
      int i,n;
      cout<<"no of students";
      cin>>n
      financial s;
      cout << "enter the following information";
      for (i=0;i<=n-1;++i)
      {
            cout<<i+1<<endl;
            s[i].getdata();
            cout<<endl;
      }
      cout<<"++++++++++++++++/n";
      cout<<"name roll sex height weight";
      cout <<"+++++++++++++++++++++/n";
      for (i=0;i<=n-1;++i)
      {
            cout<<i+1<<endl;
            s[i].display();
            cout<<endl;
      }
      cout <<endl;
      cout <<"***************/n";
      return 0;
}


SINGLE INHERITANCE:-

#include<iostream>
using namespace std;
class student
{
      private :
            char name[20];
            char  roll[26];
            char sex;
     
      public :
            void getdata();
            void displaydata();
};
class record:public student

{
      private:
                  int height;
            int weight;
      public:
            void getdata();
            void displaydata();
           
};
void student:: getdata()
{
      cout <<"enter a name /n";
      cin>> name ;
            cout <<"enter a roll name /n";
      cin>> roll;
            cout <<"enter a sex /n";
      cin>> sex;

}
void student:: displaydata()
{
      cout<<name<<"       ";
      cout << roll<< "      ";
      cout<<sex<< " ";
     
}
void record :: getdata()
{
student::getdata();
     cout <<"enter a height/n";
      cin>> height ;
     cout <<"enter a weight /n";
      cin>> weight;
}
void record :: displaydata()
{
      student ::displaydata();
      cout<< height<<"  ";
      cout<< weight<< "  ";
     
}    
int main()
{
      record s;
      cout << "enter the following information";
      s.getdata();
      cout<<"++++++++++++++++/n";
      cout<<"name roll sex height weight";
      cout <<"+++++++++++++++++++++/n";
      s.displaydata();
      cout <<endl;
      cout <<"***************/n";
      return 0;
}


SINGLE 1 BASE AND 2 DERIVED:-

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class TicTacToe
{
public:
TicTacToe();
int Pick_Player();
int Pick_Row();
int Pick_Column();
int Check_Board();
void Choice_by_Player(int);
void Choice_of_Row(int);
void Choice_of_Column(int);
void Tic_Tac_Toe_Board();
bool Check_Move (int, int);
private:
int row;
int column;
int player;
int board[3][3];
char display_board[3][3];
};
TicTacToe::TicTacToe()
{
row=0;
column=0;
player=1;
int i=0; //where i is row and j will be columns
int j=0;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
board[i][j] =0;
display_board[i][j] = ' ';
}
}
}
int TicTacToe::Pick_Player()
{
return player;
}

int TicTacToe::Pick_Row()
{
return row;
}
int TicTacToe::Pick_Column()
{
return column;
}
void TicTacToe::Choice_by_Player(int a)
{
player=a;
}
void TicTacToe::Choice_of_Row(int b)
{
row=b;
}
void TicTacToe::Choice_of_Column(int c)
{
column=c;
}
bool TicTacToe::Check_Move(int row, int column)
{
if (row!=0 && row!=1 && row!=2 )
{
cout<<"Invalid choice!!"<<endl;
return 0;
}
else if (column!=0 && column!=1 && column!=2 )
{
cout<<"Invalid choice!!"<<endl;
return 0;
}
else if (board[row][column] ==1 || board[row][column] ==2)
{
cout<<"Space already used. Try Again." <<endl;
return 0;
}

else
{
board[row][column] = player;
return 1;
}
}
int TicTacToe::Check_Board()
{
int i=0;
int j=0;
int sum= 0;
int test= 0;
int count= 0;
for (i= 0; i<3; i++)
{
sum=0;
for(j=0; j<3; j++)
{
if (board[i][j] ==0)
{
count++;
}
sum +=(board[i][j]*board[i][j]);
}
if (sum==3 ||sum==12)
{
test=sum;
break;
}
sum=0;
}
for (j=0; j<3; j++)
{
sum=0;
for (i=0; i<3; i++)
{
sum+=(board[i][j]*board[i][j]);
}
if (sum==3 || sum==12)
{
test=sum;
break;
}
sum=0;
}
if(test!=3 ||test!=12)
{
sum = (board[0][0] * board[0][0])+ (board[1][1] * board[1][1]) +
(board[2][2] * board[2][2]);
if ( sum == 3 || sum == 12)
{
test=sum;
}
}
if (test != 3 || test != 12)
{
sum = (board[2][0] * board[2][0])+ (board[1][1] * board[1][1]) +
(board[0][2] * board[0][2]);
if (sum==3||sum==12)
{
test=sum;
}
}
if (test==3)
{
test=1;
}
else if (test==12)
{
test=2;
}
else if (count==0)
{
test=3;
}
else
{
test=0;
}
return test;
}
void TicTacToe::Tic_Tac_Toe_Board()
{
for ( int row = 0; row < 3; row ++)
{
for ( int column = 0; column < 3; column++)
{
if ( board[row][column] == 0)
{
display_board[row][column] = ' ';
}
if ( board[row][column] == 1)
{
display_board[row][column] = 'X';
}
if ( board[row][column] == 2)
{
display_board[row][column] = 'O';
}
}
}
cout<<"Welcome to tic-tac-toe!"<<endl<<endl;;
cout<< " Current Player: X Current Player: O " << endl<<endl<<endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout <<display_board[0][0] << " "<<display_board[0][1]<<" | " << display_board[0][2] << " " << endl;
cout << " | | " << endl;
cout << "-----------------------------------------------" << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout <<display_board[1][0] << " "<<display_board[1][1]<<" | " << display_board[1][2] << " " << endl;
cout << " | | " << endl;
cout << "-----------------------------------------------" << endl;
cout << " | | " << endl;
cout <<display_board[2][0] << " "<<display_board[2][1]<<" | " << display_board[2][2] << " " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
}
int main()
{
TicTacToe game;
bool test;
bool more=true;
int row=0;
int column=0;
int player;
int check=0;
TicTacToe();
while (more)
{
game.Tic_Tac_Toe_Board();
player= game.Pick_Player();

cout<<" Current Player "<<player;
cout<< endl;
cout<< "Enter Row Index (0, 1, or 2): " <<endl;
cin>>row;
cout<<"Enter Column Index (0, 1, or 2): "<<endl;
cin>>column;
game.Choice_of_Row(row);
game.Choice_of_Column(column);
test= game.Check_Move( game.Pick_Row(), game.Pick_Column());
if (test==1)
{
check=game.Check_Board();
}
else
{
while (test==0)
{
cout<<"Current player"<<game.Pick_Player()<<"Invalid Choice"<<endl;
cout<<"Enter Row Index (0, 1, or 2): "<<endl;
cin>>row;
cout<<"Enter Column Index (0, 1, or 2): "<<endl;
cin>>column;
game.Choice_of_Row(row);
game.Choice_of_Column(column);
test=game.Check_Move(game.Pick_Row(), game.Pick_Column());
}
check= game.Check_Board();
}
if (check==1 ||check==1)
{
break;
}

else if (check==3)
{
game.Tic_Tac_Toe_Board();
cout<<"The game is tied. "<<endl;
}
if (player ==1)
{
player==2;
}
else
{
player=1;
}
game.Choice_by_Player(player);
}
game.Tic_Tac_Toe_Board();
cout<<"Player "<<check<<" wins! "<<endl;
return 0;
}

MULTIPLE INHERITANCE:-

#include<iostream>
using namespace std;
class basic_info
{
      private:
            char name[5];
            int roll [3];
            char sex[2];
      public:
            void getdata();
            void display();
};
class acadmics
{
      private:
            char course[5];
            int sem [3];
            int rank[2];
      public:
            void getdataA();
            void displayA();
};
class financial: public basic_info,public acadmics
{
      private:
            float amount ;
      public:
            void getdataB();
            void displayB();
           
};
void basic_info:: getdata()
{
      cout <<"enter a name /n";
      cin>> name ;
      cout <<"enter a roll name /n";
      cin>> roll;
      cout <<"enter a sex /n";
      cin>> sex;

}
void student:: displaydata()
{
      cout<<name<<"       ";
      cout << roll<< "      ";
      cout<<sex<< " ";
}
void acadmics:: getdataA()
{
      cout <<"enter a course /n";
      cin>> course ;
            cout <<"enter a sem /n";
      cin>> sem;
            cout <<"enter a rank /n";
      cin>> rank;

}
void acadmics:: displayA()
{
      cout<<course<<"       ";
      cout << sem<< "      ";
      cout<<rank<< " ";
}
void financial::getdataB()
{   
    basic_info::getdata();
    acadmics::displayA();
      cout <<"enter a amount";
      cin>>amount;
}
void basic_info::getdataB()
{
      basic_info::display();
      acadmics::displayA();
      cout<<amount <<"       ";
     
}
int main()
{
      financial s;
      cout << "enter the following information";
      s.getdata();
      cout<<"++++++++++++++++/n";
      cout<<"name roll sex height weight";
      cout <<"+++++++++++++++++++++/n";
      s.displaydata();
      cout <<endl;
      cout <<"***************/n";
      return 0;
}

INHERITANCE IN C++ INHERITANCE IN C++ Reviewed by Marketing Thrills on September 15, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.