RSS

Category Archives: Inheritance

PROGRAM TO CREATE THE INHERITANCE HIERARCHY:= PERSON -> SPOUSE

/* THIS PROGRAM CREATES THE FOLLOWING INHERITANCE HIERARCHY :-
PERSON -> SPOUSE  */

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class person {
protected:
char name[30];
long long phone;

public:
char * getname()
{ return name;}
long long getphone()
{ return phone; }
void setname(char n[])
{
strcpy(name,n);
}
void setphone(long long ph)
{ phone = ph;}
void display()
{
cout<<“\nNAME:\t”;
puts(name);
cout<<“\nPHONE:\t”<<phone<<‘\n’;
}
~person()
{}
person():phone(0)
{
strcpy(name,”NULL”);
}
person(person &p):phone(p.phone)
{
strcpy(name,p.name);
}
person(char n[], long long ph): phone(ph)
{
strcpy(name,n);
}
};

class spouse : public person
{
char spousename[20];
public:
void display()
{
cout<<“\nSPOUSE NAME:\t”;
puts(spousename);
cout<<“\nPARENT NAME:\t”;
puts(name);
cout<<“\nPH. NUMBER:\t”<<phone;
}
spouse()
{
strcpy(spousename,”NULL”);
}
spouse(char s[],char n[], long long ph):person(n,ph)
{strcpy(spousename,s);}
spouse(spouse &sp)
{  phone = sp.phone;
strcpy(name,sp.name);
strcpy(spousename,sp.spousename);
}
};

void main()
{
clrscr();
person p;
person p1(“DINESH”,938475756);
p.setname(“JAHANVI”);
p.setphone(9348757586);
person p2(p);
spouse s;
spouse s1(“RAMAN”,”MANISH”,87345738);
spouse s2(s);
cout<<“PERSONS:\n\n\n”;
cout<<“p:-\n”;
p.display();
cout<<“\np1:-\n”;
p1.display();
cout<<“\np2:-\n”;
p2.display();
getch();
clrscr();
cout<<“\nSPOUSE :\n\n\n”;
cout<<“\ns:-“;
s.display();
cout<<“\ns1:-“;
s1.display();
cout<<“\ns2:-“;
s2.display();
getch();
}

Advertisements
 
Leave a comment

Posted by on August 17, 2015 in Inheritance

 

PROGRAM TO CREATE A CLASS HIERARCHY TO MANAGE ACCOUNTS IN A BANK

// THIS PROGRAM CREATES A CLASS HIERARCHY TO MANAGE ACCOUNTS IN A BANK

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

class account {
protected:
char cname[30];
long accno;
float openingbal;

public:
void deposit(float amt)
{ openingbal += amt; }
void display();
void withdraw(float);
void read();
};

class current : private account {
long cbno;
public:
void deposit(float);
void display();
void checkmin();
void withdraw(float);
void read();
};

class savings : private account {
public:
void deposit(float);
void calcint();
void display();
void read()
{account::read();}
void withdraw(float amt)
{account::withdraw(amt);}
};

void account::display()
{
cout<<“\nACC NO: \t”<<accno;
cout<<“\n\nNAME:   \t”;
puts(cname);
cout<<“\nBALANCE:\t”<<openingbal;
}

void account::withdraw(float amt)
{
if((openingbal – amt) >= 0)
{
openingbal -= amt;
cout<<“\nTRANSACTION SUCCESSFUL\n”;
}
else
cout<<“\nNOT ENOUGH BALANCE\n”;
}

void account::read()
{
cout<<“\nACC NO:\t”;
cin>>accno;
cout<<“\nNAME:\t”;
gets(cname);
cout<<“\nBAL:\t”;
cin>>openingbal;
}
void current::deposit(float amt)
{
checkmin();
account::deposit(amt);
}

void current::read()
{
account::read();
cout<<“\nCB. NO”;
cin>>cbno;
}

void current :: display()
{
account::display();
cout<<“\nCB. NO:\t”<<cbno;
}
void current::checkmin()
{
if(openingbal <= 1000)
openingbal -= 0.09 * openingbal;
}

void current::withdraw(float amt)
{
checkmin();
account::withdraw(amt);
}

void savings::deposit(float amt)
{
calcint();
account::deposit(amt);
}

void savings::calcint()
{
openingbal = openingbal + 1.1*openingbal;
}

void savings::display()
{
account::display();
}

void main()
{
clrscr();
current c;
savings s;
cout<<“\nCURRENT ACCOUNT\n\n”;
c.read();
c.deposit(500);
c.withdraw(1000);
c.display();
cout<<“\n\nPRESS ANY KEY TO VIEW SAVINGS ACCOUNT\n\n”;
getch();
clrscr();
s.read();
s.deposit(500);
s.withdraw(1000);
s.display();
cout<<“\n\nPRESS ANY KEY TO EXIT\n”;
getch();
}

 
Leave a comment

Posted by on August 17, 2015 in Inheritance

 

PROGRAM TO SHOW MULTILEVEL INHERITANCE

//MULTILEVEL INHERITANCE: DRUG -> TABLET -> PAIN RELIEVER

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class drug
{
protected:
char category[50];
char mdate[11];
char company[50];
public:
drug();
void enter();
void show();
};

class tablet : protected drug
{
protected:
char name[50];
int vol;
float price;
tablet();
public:
void enter();
void show();
};

class painreliever : private tablet
{
int dosage;
char sideeffect[60];
int usewithin;
public:
painreliever();
void enter();
void show();
};

void main()
{
clrscr();
painreliever pain;
pain.enter();
clrscr();
pain.show();
getch();
}

void drug :: enter()
{
clrscr();
cout<<“\nEnter the Category\t\t”;
gets(category);
cout<<“\nEnter the Company\t\t”;
gets(company);
cout<<“\nEnter the manufacturing date\t”;
gets(mdate);
}

void drug :: show()
{
clrscr();
cout<<“\nCategory\t”;
puts(category);
cout<<“\nCompany \t”;
puts(company);
cout<<“\nMf. date\t”;
puts(mdate);
}

void tablet::show()
{
drug::show();
cout<<“\nName   \t\t”;
puts(name);
cout<<“\nVolume \t\t”<<vol<<‘\n’;
cout<<“\nPrice  \t\t”<<price;
}

void tablet::enter()
{
drug::enter();
cout<<“\nEnter Name \t\t\t”;
gets(name);
cout<<“\nEnter Volume \t\t\t”;
cin>>vol;
cout<<“\nEnter price\t\t\t”;
cin>>price;
}

void painreliever::enter()
{
tablet::enter();
cout<<“\nEnter dosage\t\t”;
cin>>dosage;
cout<<“\n\nEnter Side-effects\t”;
gets(sideeffect);
cout<<“\nEnter the use period\t”;
cin>>usewithin;
}

void painreliever::show()
{
tablet::show();
cout<<“\n\nDosage  \t”<<dosage<<‘\n’;
cout<<“\nSide ef \t”;
puts(sideeffect);
cout<<“\nUse pd. \t”<<usewithin<<‘\n’;
}

drug::drug()
{
strcpy(category,”UNKNOWN”);
strcpy(mdate,”00.00.0000″);
strcpy(company,”UNKNOWN”);
}

tablet::tablet()
{
strcpy(name,”NULL”);
vol = 0.0;
price = 0.0;
}

painreliever::painreliever()
{
dosage = 0;
strcpy(sideeffect,”UNKNOWN”);
usewithin = 0;
}

 
Leave a comment

Posted by on July 16, 2015 in Inheritance

 

PROGRAM TO SHOW HIERARCHIAL INHERITANCE

// PROGRAM TO SHOW HIERARCHIAL INHERITANCE

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <dos.h>
#include <process.h>

class publication
{
protected:
char title[50];
double price;

public:
void getdata();
void putdata();
};

class book : private publication
{
private:
int pagecount;
public:
void getdata();
void putdata();
book()
{ pagecount = 0;}
~book()
{}
};

class tape : private publication
{
private:
double time;
public:
void getdata();
void putdata();
tape()
{ time = 0.0; }
~tape()
{}
};

void publication :: getdata()
{
clrscr();
cout<<“Enter the title\n”;
gets(title);
cout<<“Enter the price\n”;
cin>>price;
}

void book :: getdata()
{
publication :: getdata();
cout<<“\nEnter the number of pages\n”;
cin>>pagecount;
}

void tape :: getdata()
{
publication :: getdata();
cout<<“\nEnter the duration of tape\n”;
cin>>time;
}

void publication :: putdata()
{
clrscr();
cout<<“Title\t”;
puts(title);
cout<<“\nPrice\t”;
cout<<price<<‘\n’;
}

void book :: putdata()
{
publication :: putdata();
cout<<“\nPages\t”<<pagecount;
}

void tape :: putdata()
{
publication :: putdata();
cout<<“\nTime\t”<<time;
}

void main()
{
clrscr();
cout<<“Enter your choice\n”;
cout<<“\n1. Press 1 for book\n”;
cout<<“\n2. Press 2 for Tape\n”;
cout<<“\n3. Press 3 for exit\n”;
int choice;
cin>>choice;
book b1;
tape t1;

switch(choice)
{
case 1: b1.getdata();
cout<<“\nDisplaying the details\n”;
b1.putdata();
break;
case 2: t1.getdata();
cout<<“\nDisplaying the details\n”;
t1.putdata();
break;
case 3: cout<<“\nExiting\n”;
sleep(2);
exit(0);
default: break;
}

getch();
}

 
Leave a comment

Posted by on July 16, 2015 in Inheritance

 

PROGRAM TO DERIVE TWO CLASSES (UG STUDENT AND PG STUDENT) FROM A CLASS STUDENT

// THIS PROGRAM CREATES TWO CLASSES FROM A CLASS STUDENT

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

class student {
protected:
char name[25];
int rollno;

public:
void read();
void print();
} stu;

void student::read()
{
cout<<“Enter the Name:\n”;
gets(name);
cout<<“\nEnter Roll Number:\n”;
cin>>rollno;
}

void student::print()
{
cout<<“Name:\n”;
puts(name);
cout<<“\nRoll no:\n”<<rollno;
}

class ugstudent : public student
{
int marks;

public:
void init()
{
read();
cout<<“\nEnter Marks\n”;
cin>>marks;
}
void disp()
{
print();
cout<<“\nMarks:\n”<<marks;
}
};
class pgstudent : public student
{
private:
int course;
char degree[40];
public:
void init()
{
read();
cout<<“\nEnter the Duration of Course(in years):\n”;
cin>>course;
cout<<“\nEnter the area in which you are taking degree:\n”;
gets(degree);
}
void disp();
};

void pgstudent :: disp()
{
print();
cout<<“\nDegree:\n”;
puts(degree);
cout<<“\nDuration:\n”<<course<<” years”;
}

void main()
{
ugstudent stu1;
pgstudent stu2;
clrscr();
cout<<“UG Student details:\n\n”;
stu1.init();
clrscr();
cout<<“Displaying the Details:\n\n”;
stu1.disp();
getch();
clrscr();
cout<<“PG Student Details:\n\n”;
stu2.init();
clrscr();
cout<<“Displaying the Details:\n\n”;
stu2.disp();
getch();
}

 
Leave a comment

Posted by on June 28, 2015 in Inheritance