You are on page 1of 12

EXPERIMENT NUMBER – 2.

STUDENT’S NAME: Aviral Dwivedi

STUDENT’S UID: 20BCS9316

CLASS & GROUP: 32/A

SEMESTER: 2nd

Practical 5.1: Write a program that takes information about institute staff information for

1) Teacher code, name, subject and publication

2) Officer code, name and grade

3) Typist code, name, speed and daily wages and displays it using hierarchal inheritance

PROGRAM CODE
#include <iostream>
using namespace std;
#include <string>

class staff{
public:
string code, name, subject , publication;
string offcode, offname, offgrade; string
typcode, typname;
int speed, wages;
};

class teacher : public staff {


public:
void tdetail() {
cout <<"Enter name , code , subject & publication respectively" << endl;
cin >> name >> code >> subject >> publication;
}
void tsdetail() {
cout<<" the details are :" << endl;
cout <<name << endl << code << endl << subject << endl << publication << endl;
}

};

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
class officer : public staff {
public:
void offdetail() {
cout <<"Enter officer name , code & grade respectively" << endl;
cin >> offname >> offcode >> offgrade;
}
void offsdetail() {
cout<<" the details are :" << endl;
cout <<offname << endl << offcode << endl << offgrade << endl;
}

};

class typist : public staff


{ public:
void typdetail() {
cout <<"Enter name , code , speed & wages respectively" << endl;
cin >> typname >> typcode >> speed >> wages;
}
void typsdetail() {
cout<<" the details are :" << endl;
cout << typname << endl << typcode << endl << speed << endl << wages << endl;
}

};
int main() {
int n;

cout << " **Welcome to database**"<< endl;


cout << " **Kindly choose**"<< endl << " 1.Teacher " << endl << " 2.Officer " << endl << "
3.Typist " << endl;
cin>> n;
cout <<"" <<endl;

if (n == 1)
{ teacher obj1;
obj1.tdetail();
obj1.tsdetail();
}
else if (n == 2)
{ officer obj2;
obj2.offdetail();
obj2.offsdetail();
}
else {
typist obj3;
obj3.typdetail();
obj3.typsdetail();
OBJECT ORIENTED PROGRAMMING USING
C++ LAB
}

return 0;}

Input screenshot

Output screenshot
OBJECT ORIENTED PROGRAMMING USING
C++ LAB
WHAT’S NEW

Used constructor, hierarchal inheritance.

Practical 5.2: 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 different 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 define 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.

PROGRAM CODE

#include<iostream>

using namespace std;

class student

public:

int uid;

void getnumber() {

cout<<"enter student uid"<< endl;

cin>>uid;

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
}

void putnumber() {

cout<<" student uid is " <<uid << endl;

};

class test : virtual public student {

public:

float a,b,c;

void getmarks(){

cout<<"enter subject1 marks" <<

endl; cin>>a;

cout<<"enter subject2 marks" <<

endl; cin>>b;

cout<<"enter subject3 marks" <<

endl; cin>>c;

void putmarks() {

cout<<"subject1 marks is" <<a << endl <<"subject2 marks is" <<b <<endl<<"subject1 marks is"<< c<< endl;

};

class sports : virtual public student {

public:

float sc;

void getscore(){

cout<<"enter sports marks" << endl;

cin>>sc;

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
void putscore() {

cout<<"sports marks is " << sc << endl;

};

class result: virtual public test, virtual public sports{

public:

void display()

{ putnumber(

); putmarks();

putscore();

cout<< "Total marks is " << (a+b+c+sc) << endl;

};

int main() {

result std1;

std1.getnumber();

std1.getmarks();

std1.getscore();

std1.display();

return 0;

Input screenshot

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
Output screenshot

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
WHAT’S NEW

 used multilevel inheritance


Practical 5.3: 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 float type variable to beta().Show the order of execution by invoking

constructor of derived class.

Program Code

#include<iostream>

using namespace std;

class alpha

int x;

public:

alpha(int i)

x=i;

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
cout<<"alpha initialized\n";

void show_x()

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

};

class beta

float y;

public:

beta(float j)

y=j;

cout<<"beta initialized\n";

void show_y()

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

};

class gamma : public beta,public alpha

int m,n;

public:

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

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
{

m=c,n=d;

cout<<"gamma initialized\n";

void show_mn()

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

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

};

int main()

gamma g(23,78.75,210,390);

g.show_x();

g.show_y();

g.show_mn();

return 0;

Input screenshot

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
Output screenshot

WHAT’S NEW

 used inheritance and constructor.

LEARNING OUTCOMES

 came to know about the concept of inheritance.

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
CHANDIGARH UNIVERSITY

OBJECT ORIENTED PROGRAMMING USING


C++ LAB

You might also like