0% found this document useful (0 votes)
11 views24 pages

C++ Class and Object Programs for Students and Employees

The document contains multiple C++ programming assignments focused on creating classes for different entities such as students, employees, batsmen, tests, flights, and books. Each assignment includes code snippets that demonstrate how to read and display data, perform calculations, and implement specific functionalities. The document serves as a practical guide for understanding class and object concepts in C++.

Uploaded by

riksohom3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views24 pages

C++ Class and Object Programs for Students and Employees

The document contains multiple C++ programming assignments focused on creating classes for different entities such as students, employees, batsmen, tests, flights, and books. Each assignment includes code snippets that demonstrate how to read and display data, perform calculations, and implement specific functionalities. The document serves as a practical guide for understanding class and object concepts in C++.

Uploaded by

riksohom3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name : Sohom Ghorai Regd.

No : 190301120001

C++ Assignment ( Class & Object)


1 . Write a program to create a class for student- read one student details and display it.
: Code :-
#include<iostream>
using namespace std;
class stud{
private :
char name[30];
int adm_no;
char branch[20];
char sem[10];

public:
void read(){
cout<<"Enter your Name : ";
cin>>name;
cout<<"\nEnter your admission number : ";
cin>>adm_no;
cout<<"\nEnter your branch : ";
cin>>branch;
cout<<"\nEnter your semester : ";
cin>>sem;

}
void display(){
cout << "Name : "<<name<<"\nAdmission No : "<<adm_no<<"\nBranch : "<<branch<<"\
nSemester : "<<sem<<endl ;
}
};

int main()
{

190301120001||Sohom Ghorai 1
stud s;
[Link]();
cout<<"\n\nStudent Details\n"<<endl;
[Link]();
return 0;
}

Output :
Enter your Name : Sohom

Enter your admission number : 001

Enter your branch : CSE

Enter your semester : 3rd

Student Details

Name : Sohom
Admission No : 1
Branch : CSE
Semester : 3rd

2. Write a program to create a class for student- read n student details and display it.
Code :
#include<iostream>
using namespace std;

class stu{
private:
char name[30];

190301120001||Sohom Ghorai 2
int adm;
char branch[20];
float c;
public :
void read(){
cout<<"Enter your name : ";
cin>>name;
cout<<"\nEnter your admission number : ";
cin>>adm;
cout<<"\nEnter your branch : ";
cin>>branch;
cout<<"\nEnter your CGPA : ";
cin>>c;
}
void print(){
cout<<"Name : "<<name<<"\nAdmission number : "<<adm<<"\nBranch : "<<branch<<"\
nCGPA : "<<c<<endl;
}

};

int main()
{
stu s[100];
int n,i;

cout << "Enter number of students : ";


cin >> n;

for(i=0;i<n;i++){
cout << "Enter Details of students "<< i+1 <<":\n";
s[i].read();
}
for(i=0;i<n;i++){
cout << "\nDetails of student "<<i+1<<":\n";

190301120001||Sohom Ghorai 3
s[i].print();
}
return 0;
}

Output :
Enter number of students : 2
Enter Details of students 1:
Enter your name : Sohom
Enter your admission number : 001
Enter your branch : CSE
Enter your CGPA : 8.2
Enter Details of students 2:
Enter your name : Rik
Enter your admission number : 002
Enter your branch : ME
Enter your CGPA : 7.8

Details of student 1:

Name : Sohom
Admission number : 1
Branch : CSE
CGPA : 8.2

Details of student 2:

Name : Rik
Admission number : 2
Branch : ME
CGPA : 7.8

[Link] a program to create a class for student- read n student details and display the student
datails whose percentage is grater than 80.
Code :

190301120001||Sohom Ghorai 4
#include <iostream>

using namespace std;


class student
{
private:

char name[100];

int id;

int age;
float percentage;

void check(float sala)


{
if(sala>=80)
{
disp();
}
else{
cout<<"Condition is not satisfied "<<endl;
}

public:

void read(){

cout << "enter student details:";


cout << "\n student id : ";
cin >> id;
cout << "\n name : ";
cin >> name;
cout << "\n percentage: ";
cin >> percentage;
cout << "\n age: ";

190301120001||Sohom Ghorai 5
cin >> age;
check(percentage);

void disp()
{
cout << "id : " << id<< endl;
cout << " name : " << name << endl;
cout << "salary : " << percentage << endl;
cout << "age : " << age<< endl;
}
};

int main ()
{
student s[100];
int n,i;
cout << "Enter number of students : ";
cin >> n;

for(i=0;i<n;i++){
cout << "Enter Details of students "<< i+1 <<":\n";
s[i].read();
}
return 0;
}
Output :
Enter number of students : 2
Enter Details of students 1:
enter student details:
student id : 1
name : Sohom
percentage: 88
age: 19
id : 1
name : Sohom
salary : 88
age : 19

190301120001||Sohom Ghorai 6
Enter Details of students 2:
enter student details:
student id : 2
name : Rik
percentage: 76
age: 19
Condition is not satisfied

[Link] a program to create a class for Employee (Name, Id, Salary, age )- read n employee
details and display the employee datails whose salary greater than 30000 or age greater than
35.
Code :
#include <iostream>
#include<conio.h>
using namespace std;
class Employee
{
private:

char name[100];

int id;

int age;
float salary;

void check(float sala,int ag)


{
if(sala>=30000||ag>=35)
{
disp();
}
else{

190301120001||Sohom Ghorai 7
cout<<"These conditions {Salary>=30000 or age >= 35} are not satisfied";
}
}

public:

void take()
{
cout << "enter employee details:";
cout << "\nemployee id : ";
cin >> id;
cout << "name : ";
cin >> name;
cout << "salary : ";
cin >> salary;
cout << "age: ";
cin >> age;
check(salary,age);

void disp()
{
cout << "\nid : " << id<< endl;
cout << "name : " << name << endl;
cout << "salary : " << salary << endl;
cout << "age : " << age<< endl;
}
};

int main()
{
Employee e[100];

190301120001||Sohom Ghorai 8
int n,i;
cout << "Enter number of employees : ";
cin >> n;

for(i=0;i<n;i++){
cout << "Enter Details of Employee "<< i+1 <<":\n";
e[i].take();
}

getch();
return 0;
}
Output :
Enter number of employees : 2
Enter Details of Employee 1:
enter employee details:
employee id : 1
name : Sanjay
salary : 36000
age: 37

id : 1
name : Sanjay
salary : 36000
age : 37
Enter Details of Employee 2:
enter employee details:
employee id : 2
name : Sachin
salary : 28000
age: 26
These conditions {Salary>=30000 or age >= 35} are not satisfied

190301120001||Sohom Ghorai 9
1. Define a class student with the following specification
Private members of class student
admno integer
sname 20 character
eng. math, science float
total float
ctotal() a function to calculate eng + math + science with float return type.
Public member function of class student
Takedata() Function to accept values for admno, sname, eng, science and invoke
ctotal() to calculate total.
Showdata() Function to display all the data members on the screen.

Code :
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std ;

class stud{
private :
int admno;
char sname[20];
float eng,math,science;
float total;
float ctotal()
{
return eng+math+science;
}
public :
void rd(){
cout<<"Enter Admission no : ";
cin >> admno;
cout<<"Enter name :";
cin>>sname;
cout<<"Enter English , Math and Science Marks : ";
cin>>eng>>math>>science;

190301120001||Sohom Ghorai 10
total = ctotal();

}
void pd(){
cout<<"Admission No: "<< admno <<" Name: "<< sname<<" English Marks :" <<eng<<"
Math Marks:" <<math<<" Science Marks : "<<science<<"\nTotal = "<<total;
}

};
int main()
{
stud s;
[Link]();
[Link]();
getch();
return 0;

}
Output :
Enter Admission no : 1
Enter name :Sohom
Enter English , Math and Science Marks : 87
81
88
Admission No: 1 Name: Sohom English Marks :87 Math Marks:81 Science Marks : 88
Total = 256
2. Define a class batsman with the following specifications:
Private members:
bcode 4 digits code number
bname 20 characters
innings, notout, runs integer type
batavg it is calculated according to the formula –
batavg =runs/(innings-notout)
calcavg() Function to compute batavg
Public members:
readdata() Function to accept value from bcode, name, innings, notout and

190301120001||Sohom Ghorai 11
invoke the function calcavg()
displaydata() Function to display the data members on the screen.
Code :
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;

class batsman{

int bcode;

char bname[20];

int innings,notout,runs;

int batavg;

void calcavg()

batavg=runs/(innings-notout);

public :

void readdata (){

cout<<"Enter batsman code : ";

cin>> bcode;

cout<<"Enter batsman name : ";

cin>>bname;

cout<<"enter innings ,notout and runs : ";

190301120001||Sohom Ghorai 12
cin>>innings>>notout>>runs;

calcavg();

void displaydata(){

cout<<"Batsman code : "<<bcode<<"\nBatsman name : "<<bname<<"\nInnings : "<<innings

<<"\nNot out :"<<notout<<"\nRuns :"<<runs<<"\nBatting Average :"<<batavg;

};

int main()

batsman b;

[Link]();

[Link]();

getch();

return 0;

Output :
190301120001||Sohom Ghorai 13
Enter batsman code : 7
Enter batsman name : MSD
enter innings ,notout and runs : 350
80
10773
Batsman code : 7
Batsman name : MSD
Innings : 350
Not out :80
Runs :10773
Batting Average :39
3. Define a class TEST in C++ with following description:
Private Members
TestCode of type integer
Description of type string
NoCandidate of type integer
CenterReqd (number of centers required) of type integer
A member function CALCNTR() to calculate and return the number of centers as
(NoCandidates/100+1)
Public Members
- A function SCHEDULE() to allow user to enter values for TestCode, Description,
NoCandidate & call function CALCNTR() to calculate the number of Centres
- A function DISPTEST() to allow user to view the content of all the data members
Code :
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;

class TEST{

private:

int TestCode;

char Description[30];

190301120001||Sohom Ghorai 14
int NoCandidate;

int CenterReqd;

int CALCNTR()

return NoCandidate/100+1;

public:

void SCHDULE(){

cout<<"Enter Test code : ";

cin>> TestCode;

cout<<"Enter description : ";

cin>>Description;

cout<< "Enter no of candidates : ";

cin>>NoCandidate;

CenterReqd=CALCNTR();

void DISPTEST(){

cout<<"Test code "<<TestCode<<"\nDescripton "<<Description<<"\nNo of candidate "

<<NoCandidate<<"\nCenter required "<<CenterReqd;

190301120001||Sohom Ghorai 15
};

int main ()

TEST t;

[Link]();

[Link]();

getch();

return 0;

}
Output :
Enter Test code : 123
Enter description : C++
Enter no of candidates : 120
Test code 123
Descripton C++
No of candidate 120
Center required 2

4. Define a class in C++ with following description:


Private Members
A data member Flight number of type integer
A data member Destination of type string
A data member Distance of type float
A data member Fuel of type float
A member function CALFUEL() to calculate the value of Fuel as per the following criteria
Distance Fuel
<=1000 500

190301120001||Sohom Ghorai 16
more than 1000 and <=2000 1100
more than 2000 2200
Public Members
A function FEEDINFO() to allow user to enter values for Flight Number, Destination,
Distance & call function CALFUEL() to calculate the quantity of Fuel
A function SHOWINFO() to allow user to view the content of all the data members
Code :
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class FLIGHT
{ int Fno;
char Destination[20];
float Distance, Fuel;
void CALFUEL()
{
if (Distance<=1000)
Fuel=500;
else if (Distance> 1000 && Distance<=2000)
Fuel=1100;
else if(Distance>2000)
Fuel=2200;
}
public:
void FEEDINFO(){
cout<<"Flight No : ";
cin>>Fno;
cout<<"Destination : ";
cin>>Destination;
cout<<"Distance : ";
cin>>Distance;
CALFUEL();
}
void SHOWINFO(){
cout<<"Flight No : "<<Fno<<endl;
cout<<"Destination : "<<Destination<<endl;
cout<<"Distance : "<<Distance<<endl;;

190301120001||Sohom Ghorai 17
cout<<"Fuel : "<<Fuel<<endl;;
}
};

int main()
{ FLIGHT F;
[Link]();
[Link]();
getch();
return 0;
}
Output :
Flight No : 100
Destination : Kolkata
Distance : 450
Flight No : 100
Destination : Kolkata
Distance : 450
Fuel : 500
5. Define a class BOOK with the following specifications :
Private members of the class BOOK are
BOOK NO integer type
BOOKTITLE 20 characters
PRICE float (price per copy)
TOTAL_COST() A function to calculate the total cost for N number of copies where N
is passed to the function as argument.
Public members of the class BOOK are
INPUT() function to read BOOK_NO. BOOKTITLE, PRICE
PURCHASE() function to ask the user to input the number of copies to be purchased.
It invokes TOTAL_COST() and prints the total cost to be paid by the user.
Note : You are also required to give detailed function definitions.

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

190301120001||Sohom Ghorai 18
using namespace std;

class BOOK{

int BOOKNO;

char BOOKTITLE[20];

float PRICE;

void TOTAL_COST(int N)

float tcost;

tcost=PRICE*N;

cout<<tcost;

public:

void INPUT()

cout<<"Enter Book Number : ";

cin>>BOOKNO;

cout<<"Enter Book Title : ";

cin>>BOOKTITLE;

cout<<"Enter price per copy : ";

cin>>PRICE;

190301120001||Sohom Ghorai 19
}

void PURCHASE()

int n;

cout<<"Enter number of copies to purchase : ";

cin>>n;

cout<<"Total cost is : ";

TOTAL_COST(n);

};

int main()

BOOK b;

[Link]();

[Link]();

getch();

return 0;

}
Output :
190301120001||Sohom Ghorai 20
Enter Book Number : 12345
Enter Book Title : Sherlock
Enter price per copy : 450
Enter number of copies to purchase : 20
Total cost is : 9000

6. Define a class REPORT with the following specification:


Private members :
adno 4 digit admission number
name 20 characters
marks an array of 5 floating point values
average average marks obtained
GETAVG() a function to compute the average obtained in five subject
Public members:
READINFO() function to accept values for adno, name, marks. Invoke the function
GETAVG()
DISPLAYINFO() function to display all data members of report on the screen.
You should give function definitions.

Code :
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;

class REPORT

int adno;

char name[20];

float marks[5];

190301120001||Sohom Ghorai 21
float average;

void GETAVG()

average = (marks[0]+marks[1]+marks[2]+marks[3]+marks[4])/5;

public:

void READINFO();

void DISPLAYINFO();

};

void REPORT::READINFO()

do

cout<<"Enter 4 digit admission number : ";

cin>>adno;

}while(adno<999 || adno>10000);

190301120001||Sohom Ghorai 22
cout<<"Enter name : ";

cin>>name;

cout<<"Enter marks in ";

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

cout<<"Subject "<<i+1<<":";

cin>>marks[i];

};

GETAVG();

void REPORT::DISPLAYINFO()

190301120001||Sohom Ghorai 23
cout<<"Admission number : "<<adno<<" Name : "<<name<<" Marks are : "<< marks[0]<<" "<< marks[1]

<<" "<<marks[2]<<" "<< marks[3]<<" "<< marks[4]<<" Average : "<<average;

int main()

REPORT r;

[Link]();

[Link]();

getch();

return 0;

Output :
Enter Book Number : 12345
Enter Book Title : Sherlock
Enter price per copy : 450
Enter number of copies to purchase : 20
Total cost is : 9000

190301120001||Sohom Ghorai 24

Common questions

Powered by AI

The current class implementations do not explicitly demonstrate inheritance or function overriding, as they operate mainly as standalone classes with specific objectives. To improve or incorporate inheritance, classes like 'student' or 'batsman' could be made to inherit from more generalized base classes, such as a 'Person' class that contains shared attributes like age and name. Function overriding could be introduced by defining virtual functions within these base classes and implementing specific logic in derived classes. This improvement would allow a more flexible and reusable architecture, showing more advanced object-oriented principles like polymorphism and dynamic binding .

The class 'batsman' demonstrates data encapsulation by keeping details such as bcode, innings, notout, and runs private, accessible only through public methods 'readdata' and 'displaydata'. This encapsulation prevents unintended modifications and ensures that the batting average is only calculated with the calcavg function, maintaining data integrity. Managing cricket statistics becomes more reliable as it provides a controlled environment where data consistency is enforced, calculations are accurate, and the system's integrity is safeguarded against erroneous input or operation .

Encapsulation in C++ is demonstrated in the student class by bundling the data (student details like name, admission number, branch, semester) and the methods (read and display) that operate on the data into a single unit, i.e., the class. The private members (e.g., name, adm_no) restrict direct access from outside the class, ensuring data integrity and controlled access. This allows the class to maintain control over how its data is accessed and modified, supporting the principles of information hiding and modularity .

Input validation is critical in functions like READINFO or INPUT to ensure that the data being processed is accurate and falls within expected ranges, preventing errors and improving the robustness of the application. In the provided classes, input validation is partially implemented, such as in the REPORT class: admission numbers are validated to be four digits long. However, in other parts, the validation is not thorough, as seen by accepting raw input without bounds checking. Improving input validation, like ensuring numbers fall within logical limits or strings conform to expected formats, would greatly enhance data quality and application reliability .

The private member function 'check' acts as a validator within the class structure. It determines whether specific conditions are met before certain actions are taken. For example, in the student class, it checks if the percentage is greater than or equal to 80 before displaying the student details. This affects the output by ensuring that only students meeting the criteria of having a percentage above 80 are displayed, thereby maintaining the logical flow and filtering process within the program .

The function 'TOTAL_COST' is effective in its encapsulation of the cost calculation logic within a distinct function. This practice simplifies the main codebase by delegating specific tasks to designated functions, enhancing readability and maintainability. By passing the number of copies as an argument and calculating the product of price and quantity, the function ensures accuracy and reusability of the cost calculation logic across different scenarios. Additionally, this approach supports the DRY (Don't Repeat Yourself) principle by avoiding redundancy in code .

The function 'CALFUEL' in the FLIGHT class uses decision-making constructs like if-else statements to determine the amount of fuel required based on the distance. The function checks the distance and assigns a fuel value: 500 for distances ≤1000, 1100 for distances >1000 and ≤2000, and 2200 for distances >2000. This logical division of conditions ensures precise control over fuel assignment, optimizing resource allocation in the context of flight operations. The use of these constructs is a fundamental part of controlling program flow and implementing strategic decision-making in cases with multiple potential states .

In employee data management, the 'check' function utilizes logical operators (OR in this case) to apply business rules that determine which employee details to display. By evaluating whether an employee's salary is above 30,000 or their age is 35 or higher, it filters out records that do not meet these criteria efficiently. This selective operation supports business requirements, such as focusing on higher earners or older employees for special programs, reflecting precise business logic implementation hence improving operational efficiency and decision-making processes .

Having a function like 'GETAVG' within the REPORT class offers significant advantages, such as separation of concerns where the calculation logic is distinct from input/output operations, promoting code clarity. It allows easy updates or recalibration of the average calculation without altering other parts of the class functionality. This modularity also supports reusability, enabling the function to be called independently whenever the average is needed. Moreover, it ensures consistency and accuracy in calculating grades by centralizing the logic in one place, minimizing errors from repetitive independent calculations .

In the 'TEST' class, the function CALCNTR calculates the number of test centers required based on the number of candidates. It uses the formula (NoCandidate/100 + 1) to ensure that there are adequate centers to accommodate all candidates. By automating this calculation, the function enhances resource management by providing an efficient and systematic way to allocate resources (test centers) based on real-time data. This minimizes the risk of under- or over-allocation of resources, improving logistical planning and operational efficiency .

You might also like