0% found this document useful (0 votes)
15 views

C++ Unit 2 Codes

The document discusses creating classes to represent staff information like teachers, officers, and typists using inheritance. It defines base class staff with code and name and derived classes teacher, officer, typist, and casual with additional attributes and methods. The main function allows the user to enter and display data for each category. The aim is to write a program that takes staff information and displays it using hierarchical inheritance.

Uploaded by

Ozzy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

C++ Unit 2 Codes

The document discusses creating classes to represent staff information like teachers, officers, and typists using inheritance. It defines base class staff with code and name and derived classes teacher, officer, typist, and casual with additional attributes and methods. The main function allows the user to enter and display data for each category. The aim is to write a program that takes staff information and displays it using hierarchical inheritance.

Uploaded by

Ozzy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

AIM: -Write a program that takes information about institute sta information

for
1) Teacher code, name, subject and publication
2) O cer code, name and grade
3) Typist code, name, speed and daily wages and displays it using hierarchal
inheritance

#include <iostream>

using namespace std;

class sta

protected:

int code;

char name[20];

public:

void getsta (void)

cout<<"\n\nEnter code :-";

cin>>code;

cout<<"Enter name :-";

cin>>name;

void dispsta (void)

cout<<"\nNAME :-"<<name;

cout<<"\nCODE :-"<<code;

ffi
ff
ff
ff
ff
}

};

class teacher : public sta

char sub[20];

char pub[20];

public:

void create(void)

getsta ();

cout<<"Enter Subject :-";

cin>>sub;

cout<<"Enter Publication :-";

cin>>pub;

void display(void)

dispsta ();

cout<<"\nSUBJECT :-"<<sub;

cout<<"\nPUBLICATION:-"<<pub;

};

class o cer : public sta

ffi
ff
ff
ff
ff
char grade;

public:

void create(void)

getsta ();

cout<<"Enter Grade :-";

cin>>grade;

void display(void)

dispsta ();

cout<<"\nGRADE :-"<<grade;

};

class typist : public sta

oat speed;

public:

void gettypist(void)

getsta ();

cout<<"Enter speed (wpm):-";

cin>>speed;

fl
ff
ff
ff
ff
void disptypist(void)

dispsta ();

cout<<"\nSPEED :-"<<speed;

};

class casual : public typist

oat dailywages;

public:

void create(void)

gettypist();

cout<<"Enter Daily Wages :-";

cin>>dailywages;

void display(void)

disptypist();

cout<<"\nDAILY WAGES:-"<<dailywages;

};

int main()

fl
ff
teacher o1t[10];

casual o1c[10];

o cer o1o[10];

int choice,i;

char test;

while(1)

int count;

start:

cout<<"\n=====EDUCATION INSTITUTION DATABASE=====\n\n\n";

cout<<"Choose Category of Information\n";

cout<<"1) Teachers\n";

cout<<"2) O cer\n";

cout<<"3) Typist\n";

cout<<"4) Exit\n";

cout<<"Enter your choice:-";

cin>>choice;

switch(choice)

case 1 : while(1)

cout<<"\n=====TEACHERS INFORMATION=====\n\n";

cout<<"\nChoose your choice\n";

ffi
ffi
cout<<"1) Create\n";

cout<<"2) Display\n";

cout<<"3) Jump to Main Menu\n";

cout<<"Enter your choice:-";

cin>>choice;

switch(choice)

case 1 : for(count=0,i=0;i<10;i++)

cout<<endl;

o1t[i].create();

count++;

cout<<endl;

cout<<"\n\nAre you Interested in entering data\n";

cout<<"Enter y or n:-";

cin>>test;

if(test=='y' || test=='Y')

continue;

else goto out1;

out1:

break;

case 2 : for(i=0;i<count;i++)

cout<<endl;

o1t[i].display();

cout<<endl;

break;

case 3 : goto start;

default: cout<<"\nEnter choice is invalid\ntry again\n\n";

case 2 : while(1)

cout<<"\n=====OFFICERS INFORMATION=====\n\n";

cout<<"\nChoose your choice\n";

cout<<"1) Create\n";

cout<<"2) Display\n";

cout<<"3) Jump to Main Menu\n";

cout<<"Enter your choice:-";

cin>>choice;

switch(choice)

case 1 : for(count=0,i=0;i<10;i++)

cout<<endl;

o1o[i].create();

count++;

cout<<endl;

cout<<"\n\nAre you Interested in entering data\n";

cout<<"Enter y or n:-";

cin>>test;

if(test=='y' || test=='Y')

continue;

else goto out2;

out2:

break;

case 2 : for(i=0;i<count;i++)

cout<<endl;

o1o[i].display();

cout<<endl;

break;

case 3 : goto start;

default: cout<<"\nInvalid choice\ntry again\n\n";

case 3 : while(1)

cout<<"\n=====TYPIST INFORMATION=====\n\n";

cout<<"\nChoose your choice\n";

cout<<"1) Create\n";

cout<<"2) Display\n";

cout<<"3) Jump to Main Menu\n";

cout<<"Enter your choice:-";

cin>>choice;

switch(choice)

case 1 : for(count=0,i=0;i<10;i++)

cout<<endl;

o1c[i].create();

count++;

cout<<endl;

cout<<"\n\nAre you Interested in entering data\n";

cout<<"Enter y or n:-";

cin>>test;

if(test=='y' || test=='Y')

continue;

else goto out3;

out3:

break;

case 2 : for(i=0;i<count;i++)

cout<<endl;

o1c[i].display();

cout<<endl;

break;

case 3 : goto start;

default: cout<<"\nInvalid choice\ntry again\n\n";

case 4 : goto end;

end:

return 0;

AIM: - Create a class student having student uid and


getnumber(),putnumber() as member functions to get the values and display
it. Derive a class test having marks in di erent subjects and getmarks() and
putmarks() as member functions to get and display the values. Derive another
class sports from student class having sports score and getscore(),
putscore() as member functions to get and display the values. Derive a class
result from test and sports class and de ne a function display() to calculate
total marks. Implement it with the object of result class. If it gives any error,
resolve it by adding the required functionality.

#include<iostream>

using namespace std;

class student {

int rno;

public:

void getnumber() {

cout << "Enter Roll No:";

cin>>rno;

void putnumber() {

cout << "\n\n\tRoll No:" << rno << "\n";

};

class test : virtual public student {

public:

int SUB1, SUB2;

void getmarks() {

cout << "Enter Marks\n";

cout << "SUB1:";

cin>>SUB1;

cout << "SUB2:";

cin>>SUB2;

void putmarks() {

cout << "\tMarks Obtained\n";

cout << "\n\tSUB1:" << SUB1;

cout << "\n\tSUB2:" << SUB2;

};

class sports : public virtual student {

ff
fi
public:

int score;

void getscore() {

cout << "Enter Sports Score:";

cin>>score;

void putscore() {

cout << "\n\tSports Score is:" << score;

};

class result : public test, public sports {

int total;

public:

void display() {

total = SUB1 + SUB2 + score;

putnumber();

putmarks();

putscore();

cout << "\n\tTotal Score:" << total;

};

int main()

result obj;

obj.getnumber();

obj.getmarks();

obj.getscore();

obj.display();

return 0;

Aim: - WAP to illustrate how the constructors are implemented and the order
in which they are called when the classes are inherited. Use three classes
named alpha, beta, gamma such that alpha, beta are base class and gamma
is derived class inheriting alpha &beta. Pass four variable to gamma class
object which will further send one integer variable to alpha(),one oat type
variable to beta().Show the order of execution by invoking constructor of
derived class.

#include<iostream>

using namespace std;

fl
class alpha

int x;

public:

alpha(int i)

x=i;

cout<<"alpha initialized\n";

void show_x()

cout<<"x="<<x<<"\n";

};

class beta

oat y;

public:

beta( oat j)

y=j;

cout<<"beta initialized\n";

void show_y()

fl
fl
{

cout<<"y="<<y<<"\n";

};

class gamma : public beta,public alpha

int m,n;

public:

gamma(int a, oat b,int c,int d): alpha(a),beta(b)

m=c,n=d;

cout<<"gamma initialized\n";

void show_mn()

cout<<"m="<<m<<"\n";

cout<<"n="<<n<<"\n";

};

int main()

gamma g(10,12.75,35,50);

g.show_x();

fl
g.show_y();

g.show_mn();

return 0;

AIM: -WAP to calculate and display cube of an integer and oat variable using
function overloading.
#include<iostream>

using namespace std;

int main()

int x;

oat y;

int cube(int);

oat cube( oat);

cout<<"Enter a number to nd its cube"<<endl;

cin>>x;

cout<<"The cube of the number "<<x<<" is "<<cube(x)<<endl;

cout<<"Enter a number to nd its cube"<<endl;

cin>>y;

cout<<"The cube of the number "<<y<<" is "<<cube(y)<<endl;

return 0;

int cube (int num)

return num*num*num;

oat cube ( oat num)

return num*num*num;

Aim: - Program to demonstrate the unary operator overloading for operator +


+. Make a class test. Create a default constructor to initialize the variable. 1)
Overload operator ++ (Pre) with de nition to pre-increment the value of a
variable 2) Overload operator ++ (post) with de nition to post- increment the
value of variable.

#include <iostream>

using namespace std;

fl
fl
fl
fl
fl
fi
fi
fi
fi
fl
class Integer

private:

int i;

public:

Integer(int i = 0)

this->i = i;

Integer& operator++()

++i;

return *this;

void display()

cout << "i = " << i << endl;

};

int main()

Integer i1(3);

cout << "Before increment: ";

i1.display();

Integer i2 = ++i1;

cout << "After pre increment: " << endl;

cout << "i1: ";

i1.display();

cout << "i2: ";

i2.display();

Aim:-WAP for creating a matrix class which can handle integer matrices of
di erent dimensions. Overload the operator (+) for addition and (==)
comparison of matrices.
#include<iostream>

using namespace std;

class matrix

private:long m[5][5];

int row;int col;

public:void getdata();

int operator ==(matrix);

ff
matrix operator+(matrix);

matrix operator-(matrix);

friend ostream & operator << (ostream &,matrix &);

};

int matrix::operator==(matrix cm)

if(row==cm.row && col==cm.col)

return 1;

return 0;

void matrix::getdata()

cout<<"enter the number of rows\n";

cin>>row;

cout<<"enter the number of columns\n";

cin>>col;

cout<<"enter the elements of the matrix\n";

for(int i=0;i<row;i++)

for(int j=0;j<col;j++)

cin>>m[i][j];

matrix matrix::operator+(matrix am)

matrix temp;

for(int i=0;i<row;i++)

for(int j=0;j<col;j++)

temp.m[i][j]=m[i][j]+am.m[i][j];

temp.row=row;

temp.col=col;

return temp;

matrix matrix::operator-(matrix sm)

matrix temp;

for(int i=0;i<row;i++)

for(int j=0;j<col;j++)

temp.m[i][j]=m[i][j]-sm.m[i][j];

temp.row=row;

temp.col=col;

return temp;

ostream & operator <<(ostream &fout,matrix &d)

for(int i=0;i<d.col;i++)

for(int j=0;j<d.col;j++)

fout<<d.m[i][j];

cout<<" ";

cout<<endl;

return fout;

int main()

matrix m1,m2,m3,m4;

m1.getdata();

m2.getdata();

if(m1==m2)

m3=m1+m2;

m4=m1-m2;

cout<<"Addition of matrices\n";

cout<<"the result is\n";

cout<<m3;

cout<<"subtraction of matrices\n";

cout<<"The result is \n";

cout<<m4;

else

cout<<"order of the input matrices is not identical\n";

Aim:- WAP to create a class Pairs. Objects of type Pairs can be used in any
situation where ordered pairs are needed. Our Task is to overload operator >>
and << so that objects of class Pairs are to be input and output in the form
(5,3) (5,-6) (-5,6) or (-5,-3).There is no need to implement any constructor/
method .
#include <iostream>

// for string functions

#include <cstring>

using namespace std;

class Pairs {

private:

char numpair[20];

public:

       // friend function to overload the output operator

friend ostream&operator<<(ostream&output, const Pairs &p) {

output<<p.numpair;

return output;           

       }

       // friend function to overload the input operator

friend istream&operator>>(istream&input, Pairs &p) {

char pair[20];

           // inputting the pair values

input>> pair;

           // getting the length of the inputed string

intlen = strlen(pair);

           // as per given format "(x,y)" the min length of string should be 5 and also

           // the string should contain the rst character "(", the last character ")"

           // and a comma in between the string

if (len< 5 || pair[0] != '(' || pair[len - 1] != ')' || !strstr(pair, ",")) {

cout<< "Invalid pair value found!" <<endl;

               // in case of Invalid value assing blank string to the pair value

strcpy(p.numpair, "");

           }

else {

               // in case of valid value copy the input value to the object member datq

strcpy(p.numpair, pair);

           }

return input;

       }

};

int main() {

Pairs p;

cout<< "Enter the value of pair object: ";

cin>> p;

cout<< "Entered pair value is: " << p <<endl;

return 0;

AIM: - WAP to create a class that will maintain the records of person with
details Name and Age) and nd the eldest among them. The program must
fi
fi
use this pointer to return the result by overloading> operator among two
objects.
//AIM: - WAP to create a class that will maintain the person of person with details
Name and Age) and nd the eldest among them. The program must use this
pointer to return the result by overloading> operator among two objects.

#include <iostream>

using namespace std;

class person

int age;

string name;

public:

person(){};

person(string n, int a):name(n),age(a){}

void show()

cout<<name<<" : "<<age<<endl;

person eldest(person p)

return (p.age>age)?p: *this;

};

int main()

person
PER[4]={person("ANIL",11),person("MEHAK",35),person("AJAY",30),person("OJAS
",19)};

person p;

for(int i=0;i<2;i++)

p = PER[i].eldest(PER[i+1]);

p.show();

return 0;

AIM: - WAP to access members  using pointer to object members.


#include <iostream>

using namespace std;

class Number

              private:

               int num;

              public:

               //constructor

              Number(){ num=0; };

fi
               //member function to get input

                void inputNumber (void)

                  {

               cout<<"Enter an integer number: ";

                cin>>num;

                     }

                 //member function to display number

                  void displayNumber()

                  {

                   cout<<"Num: "<<num<<endl;

                    }

};

//Main function

int main()

              //declaring object to the class number

              Number N;

              //input and display number using norn object

              N.inputNumber();

              N.displayNumber();

              //declaring pointer to the object

              Number *ptrN;

              ptrN = new Number; //creating & assigning memory

               //printing default value

              cout<<"Default value... "<<endl;

              //calling member function with pointer

              ptrN->displayNumber();

              //input values and print

              ptrN->inputNumber();

              ptrN->displayNumber();

              return 0;

Aim: - WAP to design a class representing the information regarding digital


library (books, tape: book & tape should be separate classes having the base
class as media).The class should have the functionality for adding new item,
issuing, deposit etc. The program should link the objects with concerned
function by the concept of runtime polymorphism.
#include<iostream>

#include<string.h>

using namespace std;

class media

protected:

char title[50];

oat price;

public:

fl
media(char *s, oat a)

strcpy(title, s); price = a;

virtual void display(){}

};

class book : public media

int pages; public:

book(char *s, oat a, int p) : media(s,a)

pages = p;

void display();

};

class tape : public media

oat time; public:

tape(char * s, oat a, oat t):media(s,a)

time =t;

void display();

};

void book ::display()

cout<<"\n Title:"<<title;

cout<<"\n Pages:"<<pages; cout<<"\n Price:"<<price;

void tape ::display ()

cout<<"\n Title:"<<title;

cout<<"\n Play Time:"<<time<<"mins"; cout<<"\n Price:"<<price;

int main()

char * title = new char[30]; oat price, time;

int pages;

cout<<"\n Enter Book Details \n"; cout<<"\n Title:";

cin>>title; cout<<"\n Price:"; cin>>price; cout<<"\n Pages:"; cin>>pages;

book book1(title, price, pages);

cout<<"\n Enter Tape Details";

cout<<"\n Title:";

cin>>title;

cout<<"\n Price:";

cin>>price;

cout<<"\n Play Times(mins):";

cin>>time;

fl
fl
fl
fl
fl
fl
tape tape1(title, price, time);

media* list[2];

list[0] = &book1;

list[1] = &tape1; cout<<"\n Media Details";

cout<<"\n..............Book.          ";

list[0]->display ();

cout<<"\n..............Tape.          ";

list[1]->display ();

return 0;

You might also like