Question

In: Computer Science

Taking a test on "Inheritance" in C++ tomorrow. So far we have learned about classes, structs...

Taking a test on "Inheritance" in C++ tomorrow. So far we have learned about classes, structs and arrays and strings. We will be provided with coding prompts and have to hand write codes that show examples of inheritance (most likely including some of the earlier concepts as well).

I would like some code examples such as this to study and look over with comments please.

Solutions

Expert Solution

PROGRAM 1 (Includes class and strings):

#include<iostream>
using namespace std;

class Student //holds the common details relevant to students of all streams
{
   string name;
   int roll, age;
   public:
       void input() //enters student details
       {
   cout<<"Enter name of the student : ";
   cin>>name;
   fflush(stdin);
   cout<<"Enter roll of the student : ";
   cin>>roll;
   cout<<"Enter age of the student : ";
   cin>>age;
   }
   void display() //displays student details
   {
       cout<<"\nRoll : "<<roll;
   cout<<"\nName : "<<name;
   cout<<"\nAge : "<<age;
   }
};

class Engg : public Student //inherits Student class and contains data specific to engineering students
{
   string dept;
   public:
       void input()
   {
           Student::input(); //calling the function with same name of the base class
           cout<<"Enter department (civil/mechanical/cse/electrical): ";
cin>>dept;
   }
   void display()
   {
   Student::display();
   cout<<"\nDepartment : "<<dept;
   }
};

class Science : public Student //inherits Student class and contains data specific to pure science students
{
   string dept;
   public:
   void input()
   {
   Student::input();
   cout<<"Enter department (cmsa/math/physics/chemistry): ";
cin>>dept;
   }
   void display()
   {
   Student::display();
   cout<<"\nDepartment : "<<dept;
   }
};

class Medical : public Student //inherits Student class and contains data specific to medical students
{
   string dept;
   public:
       void input()
   {
   Student::input();
   cout<<"Enter specialization (heart/skin/eyes/nerve): ";
cin>>dept;
   }
   void display()
   {
   Student::display();
   cout<<"\nSpecialization : "<<dept;
   }
};

int main()
{
   Science obj1;
   Engg obj2;
   Medical obj3;
   cout<<"\nEnter details of science student:\n";
   obj1.input();
   cout<<"\nEnter details of engineering student:\n";
   obj2.input();
   cout<<"\nEnter details of medical student:\n";
   obj3.input();
   //Illustrating virtual polymorphism
   Student *obj;
   cout<<"\nDetails of science student:";
   obj=&obj1;
   obj->display();
   cout<<"\n";
   cout<<"\nDetails of engineering student:";
   obj=&obj2;
   obj->display();
   cout<<"\n";
   cout<<"\nDetails of medical student:";
   obj=&obj3;
   obj->display();
   cout<<"\n";
}

PROGRAM 2 (Includes class, arrays, linked list and pointers):

#include<iostream>
using namespace std;

class Person //employee details program to implement inheritance
{
   protected:
       char name[50];
       int code;
   public:
       int cod;
       void inperson()
       {
           cout<<"Enter name of person: ";
           fflush(stdin);
           gets(name);
           cout<<"Enter code: ";
           cin>>code;
           cod=code;
       }
       void outperson()
       {
           cout<<"Name: ";
           puts(name);
           cout<<"Code: "<<code<<"\n";
       }
};

class Account:virtual public Person
{
   protected:
       float pay;
   public:
       void inacc()
       {
           cout<<"Enter pay: ";
           cin>>pay;
       }
       void outacc()
       {
           cout<<"Pay: "<<pay<<"\n";
       }
};

class Admin:virtual public Person
{
   protected:
       int exp;
   public:
       void inadmin()
       {
           cout<<"Enter years of experience: ";
           cin>>exp;
       }
       void outadmin()
       {
           cout<<"Experience: "<<exp<<"\n";
       }
};

class Master:public Account,public Admin
{
   Master *next;
   public:
       Master()
       {
           next=NULL;
       }
       void nextobj(Master *pt)
       {
           next=pt;
       }
       Master* retnext()
       {
           return next;
       }
};

int main() //creates a linked list of Master objects pointed to by 'start'
{
   Master *obj,*ptr,*start;
   start=NULL;
   int ch,c,k=1,up;
   while(k)
   {
       cout<<"\n1:Input\n2:Update\n3:Display\nEnter choice: ";
       cin>>ch;
       switch(ch)
       {
           case 1:
               obj=new Master;
               obj->inperson();
               obj->inacc();
               obj->inadmin();
               if(start==NULL)
               start=obj;
               else
               {
                   ptr=start;
                   while(ptr->retnext()!=NULL)
                   {
                       ptr=ptr->retnext();
                   }
                   ptr->nextobj(obj);
               }
               break;
           case 2:
               cout<<"\nEnter Code: ";
               cin>>c;
               ptr=start;
               while((ptr!=NULL) && (ptr->cod!=c))
               {
               ptr=ptr->retnext();
               }
               if(ptr==NULL)
               cout<<"Person does not exist\n";
               else
               {
                   cout<<"Update:\n1.Name & Code\n2.Pay\n3.Experience\nEnter your choice: ";
                   cin>>up;
                   if(up==1)
                   ptr->inperson();
                   else if(up==2)
                   ptr->inacc();
                   else if(up==3)
                   ptr->inadmin();
                   else
                   cout<<"Wrong input\n";
               }
          
               break;
           case 3:
               cout<<"\nEnter Code: ";
               cin>>c;
               ptr=start;
               while(ptr->cod!=c && ptr!=NULL)
               {
               ptr=ptr->retnext();
               }
               if(ptr==NULL)
               cout<<"Person does not exist\n";
               else
               {
                   ptr->outperson();
                   ptr->outacc();
                   ptr->outadmin();
               }
               break;
           default:
               cout<<"Wrong input\n";
               break;
       }
       cout<<"Press 1 to continue: ";
       cin>>k;  
   }
}


Related Solutions

What have you learned so far about what determines prices such as the rent for an...
What have you learned so far about what determines prices such as the rent for an apartment? If people cannot afford rent, what are they to do? What are the consequences of controlling rent and for not controlling it? Is the current system fair?
This question puts together all that we have learned so far. Imagine that you have just...
This question puts together all that we have learned so far. Imagine that you have just been hired as an economist by the government, and you have been asked to “create” a model that combines the goods and market service with the Asset market. We are interested in determining the relationship between output and the exchange rate. This relationship should be represented in the space with output in the horizontal axis and the exchange rate on the vertical axis, i.e....
So far we far we have been discussing the advantages of HIT, but have not considered...
So far we far we have been discussing the advantages of HIT, but have not considered its disadvantages. Discuss at least three (3) of the disadvantages or challenges that can be encountered by adoption of the HIT, such as EHR (Electronic Health Record) or HIE (health Information Exchange).
Utilizing all the information we have learned so far in this course, create your own experiment...
Utilizing all the information we have learned so far in this course, create your own experiment about a topic you are interested in. To get full credit for the assignment, you must include the following items: -What is your research question? -What is your independent variable? -What is your dependent variable? -What type of design is it (Quasi, single subject, factorial, etc) -Briefly explain the method of your study and how you would conduct the study -What would be your...
how is geometry used in Architecture and/or Engineering? Think about terms you have learned so far,...
how is geometry used in Architecture and/or Engineering? Think about terms you have learned so far, including parts of a circle and be sure to include these and examples in your discussion. Write your response in 1-2 paragraphs.
Select 5 words to summarize what you have learned so far about God, Jesus, the Gospel,...
Select 5 words to summarize what you have learned so far about God, Jesus, the Gospel, and the Bible. Take each word and write 2 sentences each explaining why you selected that particular word and how it summarizes what you've learned so far.
Reflect on and relate to what you have learned so far in the course with respect...
Reflect on and relate to what you have learned so far in the course with respect to your ability to calculate gross domestic product and its impact on the economy’s business cycle, unemployment, and inflation; using gross domestic product formulate recommendations for a government’s role in achieving full employment in an economy.
For one important and interesting idea that you have learned so far in the course based,...
For one important and interesting idea that you have learned so far in the course based, write a one page essay beginning with the following statements: Although before this class I believed …….., in fact……..Or, Although it is tempting to believe……., in fact…..Or, Although it might seem that …………… is true, in fact…… Then explain why you or others could believe the idea that is fact incorrect. In other words, what is logical or compelling about this incorrect idea. Then...
so far we have been talking about businesses and profits. But, is knowing the break-even point...
so far we have been talking about businesses and profits. But, is knowing the break-even point important only to for-profit businesses? Do non-profit entities also need to know their break-even point (if they don't make a profit)? If yes, why? Can you explain with an example? (does not need to involve numbers) [For example, Adelphi University is a nonprofit entity. Does it need to at least breakeven each year? If so, why?]
Using the information you have learned so far in this class, state what you think will...
Using the information you have learned so far in this class, state what you think will cause market fluctuations over the next few years as the economy struggles to recover.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT