You are on page 1of 12

1

EXPERIMENT NUMBER – 2.1

STUDENT’S NAME – NAMAN MALHAN


STUDENT’S UID – 22BAI70890
CLASS AND GROUP – 22AML-102(B)
SEMESTER - 2

AIM OF THE EXPERIMENT – Learn how to use classes in C++.

PROGRAM: 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

ALGORITHM:-1. Start.
2. Create a main class named staff.
3. Make subclasses such as teacher , typist , officer .
4. Enter the required information.
5. Print the result.
6. Stop.

PROGRAM:-
#include <iostream>
#include <string>
using namespace std;

// Staff class
class Staff {
protected:
int code;
string name;
public:
void get_staff_info() {
cout << "Enter code: ";
cin >> code;
cout << "Enter name: ";
cin >> name;
}
void display_staff_info() {
cout << "Code: " << code << endl;
cout << "Name: " << name << endl;

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103


2

}
};

// Teacher class
class Teacher : public Staff {
protected:
string subject;
int publication;
public:
void get_teacher_info() {
get_staff_info();
cout << "Enter subject: ";
cin >> subject;
cout << "Enter publication: ";
cin >> publication;
}
void display_teacher_info() {
display_staff_info();
cout << "Subject: " << subject << endl;
cout << "Publication: " << publication << endl;
}
};

// Officer class
class Officer : public Staff {
protected:
string grade;
public:
void get_officer_info() {
get_staff_info();
cout << "Enter grade: ";
cin >> grade;
}
void display_officer_info() {
display_staff_info();
cout << "Grade: " << grade << endl;
}
};

// Typist class
class Typist : public Staff {
protected:
int speed;
float daily_wages;
public:
void get_typist_info() {
get_staff_info();
Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103
3

cout << "Enter speed: ";


cin >> speed;
cout << "Enter daily wages: ";
cin >> daily_wages;
}
void display_typist_info() {
display_staff_info();
cout << "Speed: " << speed << endl;
cout << "Daily wages: " << daily_wages << endl;
}
};

int main() {
Teacher t;
Officer o;
Typist ty;

cout << "Enter teacher information:" << endl;


t.get_teacher_info();
cout << endl;

cout << "Enter officer information:" << endl;


o.get_officer_info();
cout << endl;

cout << "Enter typist information:" << endl;


ty.get_typist_info();
cout << endl;

cout << "Teacher information:" << endl;


t.display_teacher_info();
cout << endl;

cout << "Officer information:" << endl;


o.display_officer_info();
cout << endl;

cout << "Typist information:" << endl;


ty.display_typist_info();
cout << endl;

return 0;
}

ERRORS OCCURRED – The program was not executing properly.

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103


4

PROGRAM’S EXPLANATION (in brief) - This program is a simple C++ language program used
to explain the concept of inheritance .

The following steps must be followed to make the program work:

1] Begin by opening a header file.

2] Write the main ( ) function, which is a must-have function.

3] Provide the conditions and find the solution to the problem.

4] To retrieve the output from the arithmetic method, select the print option.

OUTPUT:-

LEARNING OUTCOMES:–

 This practical introduced me to the fundamentals of the C++ programming language.

 Understanding the ways of execution and debugging the programs in C++ language.

 It provides a better outlook of course.

 Helps to build and compile a simple C++ language program to print information of a student.

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103


5

PRACTICAL – 2.1(b)

AIM OF THE EXPERIMENT – Learn how to use classes in C++.

PROGRAM: Create a class student with student uid and getnumber() and putnumber() as member
functions to get the values and display them. 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 the result class. If it gives any
error, resolve it by adding the required functionality.

ALGORITHM:-1. Start.
2. Create a main class named student.
3. Make subclasses such as test, sports, result.
4. Enter the required information.
5. Print the result.
6. Stop.

PROGRAM:-
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
string UID;
public:
void getnumber()
{
cout<<("\n Enter UID of Student:- ");
cin>>UID;
}
void putnumber()
{
cout<<("\n The UID of the student is:- ")<<UID;
}
};
class test: virtual public student

{
public:
int marks1;
int marks2;
void getmarks()
{
Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103
6

cout<<("\n Enter marks of student for subject_1 :- ");


cin>>marks1;
cout<<("\n Enter marks of student for subject_2 :- ");
cin>>marks2;
}
void putmarks()
{
cout<<("\n Marks obtained are:- ")<<("\n Subject_1 :-
")<<marks1<<("\n Subject_2 :- ")<<marks2;
}
};
class sports: public virtual student
{
public:
int score;
void getscore()
{
cout<<("\n Enter scores obtained in Sports:- ");
cin>>score;
}
void putscore()
{
cout<<("\n Scores in Sports are:- ")<<score;
}
};
class result: public test, public sports
{
public:
int total;
void display()
{
total= marks1 + marks2+ score;
putnumber();
putmarks();
putscore();
cout<<("\n Total scores of Student:- ")<< total;
}
};

int main()
{ result stud;
stud.getnumber();
stud.getmarks();
stud.getscore();
stud.display();
Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103
7

return 0;
}

ERRORS OCCURRED – The program was not executing properly.

PROGRAM’S EXPLANATION (in brief) - This program is a simple C++ language program used
to explain, a simple and parameterized constructor.

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103


8

The following steps must be followed to make the program work:

1] Begin by opening a header file.

2] Write the main ( ) function, which is a must-have function.

3] Provide the conditions and find the solution to the problem.

4] To retrieve the output from the arithmetic method, select the print option.

OUTPUT:-

LEARNING OUTCOMES:–

 This practical introduced me to the fundamentals of the C++ programming language.

 Understanding the ways of execution and debugging the programs in C++ language.

 It provides a better outlook of course.

 Helps to build and compile a simple C++ language program to print information of a student.

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103


9

PRACTICAL –2.1(c)

AIM OF THE EXPERIMENT – Learn how to use classes in C++.

PROGRAM:- Write a program 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 classes and gamma is the derived classes inheriting alpha
&beta. Pass four variables to the 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 the constructor of the derived class.

ALGORITHM – 1. Start.
2. Make two main classes named alpha and beta.
3. Now make an inherited class gamma from the alpha and beta class.
4. Enter the integers and float numbers.
5. Print the result.
6. Stop.

PROGRAM CODE:–

#include<iostream>
using namespace std;
class alpha
{
int x;
public:
alpha(int i)
{
x=i;
cout<<"\n alpha initialized\n";
}
void show_x()
{
cout<<"x="<<x<<"\n";
}
};
class beta
{
float y;
public:
beta(float j)
{
y=j;
cout<<"\n beta initialized\n";
}

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103


10

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)
{
m=c,n=d;
cout<<"\n gamma initialized\n\n";
}

void show_mn()
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};

int main()
{
int i,m,n;
float j;
cout<<("\n Enter three integer values:- ");
cin>>i>>m>>n;
cout<<("\n Enter one float value:- ");
cin>>j;
gamma g(i,j,m,n);
g.show_x();
g.show_y();
g.show_mn();
return 0;
}

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103


11

ERROR OCCURRED:- No Error Occurred.

PROGRAM’S EXPLANATION (in brief) - This program is a simple C++ language program, using
a constructor and destructor.

The following steps must be followed to make the program work:

1] Begin by opening a header file.


2] Write the main ( ) function, which is a must-have function.
3] Take the number and write down the program’s input as well. As well as the variables.
4] To retrieve the output from the arithmetic method, select the print option.

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103


12

RESULT –

LEARNING OUTCOMES –

 This practical introduced me to the fundamentals of the C++ programming language.

 Understanding the ways of execution and debugging the programs in C++ language.

 It provides a better outlook of course.

 Helps to build and compile a simple C++ language program for use of the inherited class.

EVALUATION COLUMN (To be filled by concerned faculty only) -

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Worksheet Completion 10

2. Viva 8

3. Conduct 12

Total Marks 30

Subject :- Object Oriented Programming Using C++ SUBJECT CODE:- 22CSH-103

You might also like