0% found this document useful (0 votes)
8 views23 pages

C++ Class Examples: Box, Student, Employee

The document contains multiple C++ code examples demonstrating various programming concepts including classes, static variables, constructors, virtual functions, operator overloading, exception handling with try-catch, file handling, and templates. Each section provides a brief overview of the functionality and purpose of the code, showcasing how to implement these concepts in C++. The examples illustrate practical applications such as calculating volumes, managing student data, employee earnings, complex number operations, hotel booking validations, and sorting algorithms.

Uploaded by

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

C++ Class Examples: Box, Student, Employee

The document contains multiple C++ code examples demonstrating various programming concepts including classes, static variables, constructors, virtual functions, operator overloading, exception handling with try-catch, file handling, and templates. Each section provides a brief overview of the functionality and purpose of the code, showcasing how to implement these concepts in C++. The examples illustrate practical applications such as calculating volumes, managing student data, employee earnings, complex number operations, hotel booking validations, and sorting algorithms.

Uploaded by

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

Class -

#include <iostream>
using namespace std;

class Box
{
public:
double length,breadth,height;
double getVolume(void)
{
return length*breadth*height;
}
double getSurfaceArea(void);
};

double Box::getSurfaceArea(void)
{
return 2*length*breadth + 2*breadth*height + 2*height*length;
}

int main()
{
Box Box1;

[Link] = 2.5;
[Link] = 5.6;
[Link] = 8.7;

cout<<"Volume of Box : "<<[Link]()<<endl;


cout<<"Surface Area of Box : "<<[Link]()<<endl;

return 0;
}
1. Static Variable -

#include <iostream>
using namespace std;

class Student
{
public:
string name, divi, blood, address;
int roll, clas, dob, marks1, marks2;
long long tele, aadhar;

friend void percent(Student s1); //friend function declaration

static int count ; //static variable declaration

void getInfo()
{
cout<<"Enter name";
cin>>name;
cout<<"Enter division";
cin>>divi;
cout<<"Enter aadhar";
cin>>aadhar;
cout<<"Enter bloodgroup";
cin>>blood;
cout<<"Enter address";
cin>>address;
cout<<"Enter roll no";
cin>>roll;
cout<<"Enter class";
cin>>class;
cout<<"Enter date of birth";
cin>>dob;
cout<<"Enter telephone";
cin>>tele;
cout<<"Marks1";
cin>>marks1;
cout<<"Marks2";
cin>>marks2;
count++;
}

void Display()
{
cout<<"Name-"<<name<<endl;
cout<<"Divison-"<<divi<<endl;
cout<<"Bloodgroup-"<<blood<<endl;
cout<<"Address-"<<address<<endl;
cout<<"Roll no-"<<roll<<endl;
cout<<"Class-"<<clas<<endl;
cout<<"Date of Birth-"<<dob<<endl;
cout<<"Telephone number-"<<tele<<endl;
}

};

int Student::count=0; //setting static variable

void percent (Student s1)


{
cout << "Subject 1 marks : " << s1.marks1 << endl;
cout << "Subject 2 marks : " << s1.marks2 << endl;
cout << "Percentage: " << (s1.marks1+s1.marks2)/2<< "%" << endl;
}

int main ()
{
Student S[20];
char choice = ‘Y’;
int i = 0;

while(choice == 'Y')
{
S[i].input();
i++;

cout<<"Enter another student's data? (Y/N) ";


cin>>choice;
}

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


{
S[j].display();
percent(S[j]);
}

cout<< "Total number of students are "<<Student::count;

return 0;
}
2. Constructor Deconstructor –

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

class construct
{
public :
string name;
int roll;
string clas;
string divi;
double contact;
double aadhar;
string bldgrp;
string addr;

construct()
{
cout<<endl;
cout<<"Default Constructor - "<<endl;
name = "Priyanka";
roll = 21;
clas = "SY";
divi = "C";
contact = 12345;
};

void showdata()
{
cout<<"The name is -"<<name<<endl;
cout<<"The roll number is - "<<roll<<endl;
cout<<"The class is - "<<clas<<endl;
cout<<"The division is - "<<divi<<endl;
cout<<"The contact is - "<<contact<<endl;
cout<<endl;
};

construct(string nam,int rol, string cla, string di,double cont)


{
name = nam;
roll = rol;
clas = cla;
divi = di;
contact = cont;
cout<<endl;
cout<<"Parametrized constructor -"<<endl;

};

construct(construct &p)
{
cout<<endl<<"Copy constructor -"<<endl;
name = [Link];
roll = [Link];
clas = [Link];
divi = [Link];
contact = [Link];

};

~construct()
{
cout<<"Destructor Executed"<<endl;
};
};

int main()
{
construct s1;
[Link]();
construct s2 ("Abc",25,"FY","C",96376);
[Link]();
construct s3 (s1);
[Link]();
return 0;
}
3. Virtual Function -

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

class Employee
{
public:
string empname;
int empid;
int wage;

Employee()
{
empname = "priyanka";
empid = 203021;
}

Employee(string name,int id)


{
empname = name;
empid = id;
}

void input()
{
cout<<endl<<"Enter employee name: ";
cin>>empname;
cout<<"Enter employee ID: ";
cin>>empid;
}

void display()
{
cout<<endl<<"Employee "<<empname<<endl;
cout<<"ID "<<empid<<endl;
}

virtual void Earnings() = 0;


};
class SalaryEmployee : public Employee
{
public:
double weeklysalary;

SalaryEmployee() : Employee(), weeklysalary(500){}

SalaryEmployee(string name, int id, double wsalary):Employee(name,id)


{
empname = name;
empid = id;
weeklysalary = wsalary;
}

void Earnings()
{
cout<<"Salary for Employee "<<empid<<" is "<<weeklysalary<<endl;
}

};

class HourlyEmployee : public Employee


{
public:
double wage, hours, hourlysalary;

HourlyEmployee() : Employee()
{
wage = 20;
hours = 10;
hourlysalary = 50;
}

HourlyEmployee(string name, int id, double w, double hrs) : Employee(name,id)


{
wage = w;
hours = hrs;
hourlysalary = 0;
}
void Earnings()
{
if (hours < 40)
hourlysalary = hours*wage;
else
hourlysalary = 40 * wage + ((hours - 40) * wage * 1.5);

cout<<"Salary for Employee "<<empid<<" is "<<hourlysalary<<endl;


}
};

class CommissionEmployee : public Employee


{
public:
double GrossSales, CommissionRate, CommissionSalary;

CommissionEmployee() : Employee()
{
GrossSales = 5;
CommissionRate = 100;
CommissionSalary = 1000;
}

CommissionEmployee(string name, int id, double salary) : Employee(name, id)


{
GrossSales = salary;
CommissionRate = 0.1;
CommissionSalary = 0;
}

void Earnings()
{
CommissionSalary = GrossSales * CommissionRate;

cout<<"Salary for Employee "<<empid<<" is "<<CommissionSalary<<endl;


}
};

int main()
{

int choice;
cout<<"Enter type of Employee- \n 1. Salaried Employee \n 2. Hourly Employee \n 3.
Commissioned Employee \n 4. Exit"<<endl;

cin>>choice;

switch(choice)
{
case 1:
{
SalaryEmployee S1;
[Link]();
[Link]();
[Link]();
SalaryEmployee S2("john",02,560);
[Link]();
[Link]();
break;
}

case 2:
{
HourlyEmployee H1;
[Link]();
[Link]();
[Link]();
HourlyEmployee H2("reyna",04,4500,25);
[Link]();
[Link]();
break;
}

case 3:
{
CommissionEmployee C1;
[Link]();
[Link]();
[Link]();
CommissionEmployee C2("priyanka",06,90000);
[Link]();
[Link]();
break;
}

default:
cout<<"System Exited."<<endl;

4. Operator Overloading –

#include <iostream>
using namespace std;

class complex
{
float x; //real
float y; //imaginary

public:

complex() { }

complex(float real, float imag)


{
x = real;
y = imag;
}

complex operator +(complex);

friend complex operator-(complex c1, complex c2);

complex operator *(complex);

friend complex operator/(complex c1, complex c2);

void display(void);

};
complex complex :: operator+(complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return (temp);
}

complex operator-(complex ca, complex cb)


{
complex temp2;
temp2.x = ca.x - cb.x;
temp2.y = ca.y - cb.y;
return (temp2);
}

complex complex :: operator*(complex c2)


{
complex temp3;
temp3.x = (x * y) - (c2.x * c2.y);
temp3.y = (x * c2.y) + (c2.x * y);
return (temp3);
}

complex operator/(complex c3, complex c4)


{
complex temp4;
temp4.x = ((c3.x * c4.x) + (c3.y * c4.y))/((c4.x * c4.x) + (c4.y * c4.y));
temp4.y = ((c3.y * c4.x) - (c3.x * c4.y))/((c4.x * c4.x) + (c4.y * c4.y));
return (temp4);
}

void complex :: display(void)


{
cout<<x<<" + "<<y<<"i\n";
}

int main()
{
float a, b, c, d;
complex C1, C2, C3;
cout<<endl<<"Enter real part of 1st number- ";
cin>>a;
cout<<endl;
cout<<"Enter imaginary part of 1st number- ";
cin>>b;
cout<<endl;
cout<<"Enter real part of 2nd number- ";
cin>>c;
cout<<endl;
cout<<"Enter imaginary part of 2nd number- ";
cin>>d;
cout<<endl;

C1 = complex(a,b);
C2 = complex(c,d);

int choice;
char ch2 = 'Y';

cout<<"C1 = ";
[Link]();
cout<<"C2 = ";
[Link]();

while(ch2=='Y')
{

cout<<"\nEnter 1 for Addition, 2 for Subtraction, 3 for Multiplication or 4 for


Division"<<endl;
cin>>choice;

switch(choice)
{
case 1:
C3 = C1 + C2;
cout<<"C3 = ";
[Link]();
break;

case 2:
C3 = C1 - C2;
cout<<"C3 = ";
[Link]();
break;

case 3:
C3 = C1 * C2;
cout<<"C3 = ";
[Link]();
break;

case 4:
C3 = C1 / C2;
cout<<"C3 = ";
[Link]();
break;
}

cout<<"Perform another operation? (Y/N)"<<endl;


cin>>ch2;
}

return 0;
}
5. Try Catch Throw –

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

class hotel
{
public:
int age;
int cust_id = 1;
int income;
int count = 0;
string name;
string city;
string room_type;

hotel()
{
age = 25;
cust_id = 0;
income = 30000;
name = "Priyanka";
city = "Pune";
room_type = "2 Beds";
}

void accept()
{
cout<<"\nEnter customer's name - ";
cin>>name;
getAge();
getIncome();
getCity();
getRoom();
}

void display()
{
if ( count == 4 )
{
cout<<"\nCustomer ID - "<<cust_id;
cout<<"\nCustomer's name - "<<name;
cout<<"\nCustomer's age - "<<age;
cout<<"\nCustomer's income - "<<income;
cout<<"\nCity - "<<city;
cout<<"\nRoom booked - "<<room_type;
}

else
{
cout<<"\nRoom booking unsuccessful.\n";
}
}

void getAge()
{
int Age;
try
{
cout<<"\nEnter customer's age - ";
cin>>Age;

if (Age > 18 && Age < 55)


{
count++;
age = Age;
}

else
{
throw Age;
}

catch (int Age)


{
cout<<"Age not valid\n";
}
}
void getIncome()
{
int Inc;
try
{
cout<<"\nEnter customer's income - ";
cin>>Inc;

if (Inc >= 50000 && Inc <= 100000)


{
count++;
income = Inc;
}

else
{
throw Inc;
}

catch (int Inc)


{
cout<<"Income not sufficient!\n";
}
}

void getCity()
{
string City;
try
{
cout<<"\nEnter city to book - ";
cin>>City;

if (City == "Pune" || City == "Mumbai")


{
count++;
city = City;
}
else
{
throw City;
}

catch (string City)


{
cout<<"Not available in that city!\n";
}
}

void getRoom()
{
string Room;
try
{
cout<<"\nEnter room preference - ";
cin>>Room;

if (Room == "Deluxe" || Room == "Super Deluxe")


{
throw Room;
}

else
{
count++;
room_type = Room;
}

catch (string Room)


{
cout<<"Room type not available!\n";
}
}
};

int main()
{
hotel R2;
[Link]();
[Link]();

hotel R3;
[Link]();
[Link]();
}
6. File Handling –

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void count(string para);

int main()
{

string para;

ofstream f1;
[Link]("[Link]");

cout<<"\nEnter contents of paragraph - \n";


getline(cin,para);
f1<<para;

[Link]();

ofstream f2;
[Link]("[Link]");
f2<<para;

count(para);

[Link]();

void count(string str)


{
int up = 0, dig = 0, space = 0, vow = 0, i = 0, count = 0;
while (i < [Link]())
{
if (str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U')
{
up++;
vow++;
}

else if(str[i] =='a' || str[i] =='e' || str[i] =='i' || str[i] =='o' || str[i] =='u')
vow++;

else if(str[i] >= '0' && str[i] <= '9')


dig++;

else if(str[i] == ' ')


{
space++;
count--;
}

else if(str[i] >= 'A' && str[i] <= 'Z')


up++;

i++;
count++;
}

cout<<"\nNumber of uppercase letters : "<<up<<endl;


cout<<"\nNumber of digits : "<<dig<<endl;
cout<<"\nNumber of white spaces : "<<space<<endl;
cout<<"\nNumber of vowels : "<<vow<<endl;
cout<<"\nTotal number of characters : "<<count<<endl;

}
7. Template –

#include <iostream>
using namespace std;

template <class B>


void bubblesort(B arr[5])
{
B temp;
for(int i=0; i<4; i++)
{
for(int j=0; j<4-i; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

int main()
{
cout<<"Enter array of 5 integers -"<<endl;
for(int k=0; k<5; k++)
{
cin>>arr1[k];
}

bubblesort(arr1);

cout<<endl<<"Sorted array of 5 integers -"<<endl;


for(int k=0; k<5; k++)
{
cout<<arr1[k]<<" ";
}

cout<<endl<<endl<<"Enter array of 5 floats -"<<endl;


for(int k=0; k<5; k++)
{
cin>>arr2[k];
}

bubblesort(arr2);

cout<<endl<<"Sorted array of 5 floats -"<<endl;


for(int k=0; k<5; k++)
{
cout<<arr2[k]<<" ";
}

return 0;
}

You might also like