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

C++ Assignment (Class and Object)

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C++ Assignment (Class and Object)

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

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;
s.read();
cout<<"\n\nStudent Details\n"<<endl;
s.display();
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

3.Write 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

4.Write 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;
s.rd();
s.pd();
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;

b.readdata();

b.displaydata();

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;

t.SCHDULE();

t.DISPTEST();

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;
F.FEEDINFO();
F.SHOWINFO();
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;

b.INPUT();

b.PURCHASE();

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;

r.READINFO();

r.DISPLAYINFO();

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

You might also like