OOPS
OOPS
ASSINGMENT- 2/3
Q.1 Write a c++ program to create class person with data member s(Name
,Age).
Create a class employee inherited from person with data
members(eid,salary).
Create a class programmer inherited from Employee with data no-of-lang
known.
Show the detail of Programmer.
#include<iostream.h>
#include<conio.h>
class person
{
protected:
char name[50];
int age;
public:
void per_getdata()
{
cout<<"Enter name : ";
cin.getline(name,50);
cout<<"Enter age : ";
cin>>age;
}
void per_putdata()
{
cout<<"Name : "<<name<<endl;
cout<<"Age : "<<age<<endl;
}
};
class Employee:public person
{
protected:
int eid;
float salary;
public:
void emp_getdata()
{
cout<<"Enter employee ID : ";
cin>>eid;
cout<<"Enter salary : ";
cin>>salary;
}
void emp_putdata()
{
cout<<"Employee ID : "<<eid<<endl;
cout<<"Salary : "<<salary<<endl;
}
};
class Programmer:public Employee
{
private:
int numOfLangknown;
public:
void pro_getdata()
{
cout<<"Enter number of language known : ";
cin>>numOfLangknown;
}
void pro_putdata()
{
cout<<"Number of language known : "<<numOfLangknown<<endl;
}
};
int main()
{
clrscr();
Programmer p1;
p1.per_getdata();
p1.emp_getdata();
p1.pro_getdata();
cout<<"\n :: Programmer Details :: \n";
p1.per_putdata();
p1.emp_putdata();
p1.pro_putdata();
getch();
}
Output :
Enter name : Tanisha
Enter age : 19
Enter employee ID : 111
Enter salary : 45000
Enter number of language know : 3
:: Programmer Details ::
Name : Tanisha
Age : 19
Employee ID : 111
Salary : 45000
Number of language know : 3
Q.2 Write a c++ program to create two class “Fahrenheit’ and Celsius, which
takes input in Fahrenheit and convert the temperature into Celsius.
Note : C=(F-32)*5/9
#include<iostream.h>
#include<conio.h>
class Celsius
{ private:
double temperature;
public:
Celsius(double temp) : temperature(temp)
{
}
double getTemperature()
{
return temperature;
}
void convertToFahrenheit()
{
temperature = (temperature * 9 / 5) + 32;
}
};
class Fahrenheit
{ private:
double temperature;
public:
Fahrenheit(double temp) : temperature(temp)
{
}
double getTemperature()
{
return temperature;
}
void setTemperature(double temp)
{
temperature = temp;
}
void convertToCelsius()
{
temperature = (temperature - 32) * 5 / 9;
}
};
int main()
{
clrscr();
double fahrenheitTemp, celsiusTemp;
cout << "Enter temperature in Fahrenheit: ";
cin >> fahrenheitTemp;
Fahrenheit fahrenheitObj(fahrenheitTemp);
fahrenheitObj.convertToCelsius();
celsiusTemp = fahrenheitObj.getTemperature();
cout << "Equivalent temperature in Celsius: " << celsiusTemp<< endl;
cout << "Enter temperature in Celsius: ";
cin >> celsiusTemp;
Celsius celsiusObj(celsiusTemp);
celsiusObj.convertToFahrenheit();
fahrenheitTemp = celsiusObj.getTemperature();
Output :
Enter temperature in Fahrenheit : 96.8
Equivalent temperature in Celsius : 36
Enter temperature in Celsius : 36
Equivalent tempreture in Fahrenheit : 96.8
Q.3 Write a program to create a class student stores the roll_no ,class test
stores the mark obtained in two subject and class result contains the total
marks obtained the test . The class result can inherit the details of the marks
obtained in the test and the roll_no of student class.
#include <iostream.h>
#include<conio.h>
class Student
{
protected: int roll_no;
public:
Student(int roll) : roll_no(roll)
{
}
int getRollNo()
{
return roll_no;
}
};
class ClassTest
{
protected:
int subject1Marks;
int subject2Marks;
public:
ClassTest(int marks1, int marks2) : subject1Marks(marks1),
subject2Marks(marks2)
{
}
int getSubject1Marks()
{
return subject1Marks;
}
int getSubject2Marks()
{
return subject2Marks;
}
};
class Result : public Student, public ClassTest
{ private:
int totalMarks;
public:
int getTotalMarks()
{
return totalMarks;
}
};
int main()
{ clrscr();
int roll_no, marks1, marks2;
Output:
Enter roll number : 20
Enter marks for subject 1 : 75
Enter marks for subject 2 : 80
Roll Number : 20
Subject 1 Marks : 70
Subject 2 Marks : 80
Total Marks : 150
Q.4 Write a program for creating a class figure & calculate the area of the
circle
(pi*r*r) with the help of constructor.
Get the details from the user & display it .
#include<iostream.h>
#include<conio.h>
const double PI = 3.14159265359;
class Figure
{
private:
double radius;
double area;
public:
Figure(double r) : radius(r)
{
double getRadius()
{
return radius;
}
double getArea()
{
return area;
}
};
int main()
{
clrscr();
double circleRadius;
cout << "Enter the radius of the circle: ";
cin >> circleRadius;
Figure circle(circleRadius);
cout << "Circle Details:" << endl;
cout << "Radius: " << circle.getRadius() << endl;
cout << "Area: " << circle.getArea() << endl;
getch();
}
Output :
Circle Details :
Radius : 5
Area : 78.539816
Q.5 Create an abstract class “Shape” which stores data members like length,
breadth and radius. Create two classes “Circle “and “rectangle” which stores
data members like area Respectively.Write a function to calculate area and
display it.
#include<iostream.h>
#include<conio.h>
public:
virtual void calculateArea() = 0;
virtual void displayArea() = 0;
};
void displayArea() {
float area = 3.1415 * radius * radius;
cout << "Area of the circle: " << area << endl;
}
};
void displayArea() {
float area = length * breadth;
cout << "Area of the rectangle: " << area << endl;
}
};
int main() {
clrscr();
Shape* shape;
Circle c;
shape = &c;
shape->calculateArea();
shape->displayArea();
Rectangle r;
shape = &r;
shape->calculateArea();
shape->displayArea();
getch();
return 0;
}
Output :
Enter the radius of the circle : 5
Area of the circle : 78.537498
Enter the length of the rectangle : 3
Enter the breadth of the rectangle : 3
Area of the rectangle : 9
class MyString {
private:
char* str;
public:
MyString(const char* s) {
str = new char[strlen(s) + 1];
strcpy(str, s);
}
~MyString() {
delete[] str;
}
void display() {
cout << str << endl;
}
int main() {
clrscr();
MyString s1("Patel");
MyString s2("Tannu");
MyString s3("Patel ");
MyString s4 = s1 + s2;
s1.display();
s2.display();
s3.display();
s4.display();
if (strcmp(s1.c_str(), s2.c_str()) == 0) {
cout <<endl<< "s1 and s2 are equal." << endl;
} else {
if (strcmp(s1.c_str(), s3.c_str()) == 0) {
cout << "s1 and s2 are not equal." << endl;
} else {
cout << "s1 and s3 are equal." << endl;
}
getch();
}
Output :
Patel
Tannu
Patel
PatelTannu
{
}
Value entered : 10
➢ Ans:-
#include <iostream.h>
#include <string.h>
#include<conio.h>
// to concatinate string
class AddString {
public:
// to compaire string
class CompareString {
public:
char str[25];
CompareString(char str1[])
{
strcpy(this->str, str1);
}
int operator==(CompareString s2)
{
if (strcmp(str, s2.str) == 0)
return 1;
else
return 0;
}
if (s1 == s2)
cout << s1.str << " is equal to "
<< s2.str << endl;
else {
cout << s1.str << " is not equal to "
<< s2.str << endl;
}
}
void testcase1()
{
CompareString s1(str1);
CompareString s2(str2);
cout << "Comparing \"" << s1.str << "\" and \""
<< s2.str << "\"" << endl;
compare(s1, s2);
}
void testcase2()
{
char str1[] = "Geeks";
char str2[] = "Geeks";
CompareString s1(str1);
CompareString s2(str2);
cout << "\n\nComparing \"" << s1.str << "\" and \""
<< s2.str << "\"" << endl;
compare(s1, s2);
}
int main()
{
clrscr();
testcase1();
testcase2();
char str1[] = "Patel";
char str2[] = "Tanu";
+a1;
getch();
}
Output:
Comparing “Geeks” and “Geeks”
Geeks is not equal to Geeks.
Q.9 Use + operator to concate two string. Use = operator to copy one string
to another string.
#include<iostream.h>
#include<string.h>
#include<conio.h>
class string
{
char s[100]; public: string()
{
strcpy(s,"");
}
string(char s1[])
{
strcpy(s,s1);
}
void display()
{
cout<<s<<endl;
}
string operator +(string str)
{
string temp;
strcpy(temp.s,s);
strcat(temp.s,str.s);
return temp;
}
};
void main()
{
clrscr();
string fstr="Patel";
string sstr="Tanu";
string tstr;
fstr.display();
sstr.display();
tstr=fstr+sstr;
cout<<"Concatenation : ";
tstr.display();
getch();
}
Output :
Patel
Tanu
Concatenation : PatelTanu
Q.10 Design two classes that support following type conversions : rupees R ;
Dollar D;D=R; Converts Rupee to dollar. Write a program to carry out this
conversion.
#include<iostream.h>
#include<conio.h>
class Dollar;
class Rupees
{
private:
float amount;
public:
Rupees(float amt) : amount(amt)
{
}
operator Dollar();
void display()
{
cout<< "Rupees: "<<amount<< endl;
}
};
class Dollar
{ private:
float amount;
public:
Dollar(float amt) : amount(amt)
{
}
void display()
{
cout << "Dollars: " << amount << endl;
}
operator Rupees()
{
return Rupees(amount*75);
}
};
Rupees::operator Dollar()
{
return Dollar(amount/75);
}
int main()
{
clrscr();
Rupees r(1500);
Dollar d = r;
r.display();
d.display();
Rupees r2 = d;
getch();
}
Output :
Rupees : 1500
Dollars : 20
Q.11 Write a Program to add two time object using operator overloading.
#include<iostream.h>
#include<conio.h>
class Time
{
int hours;
int minutes;
public:
Time()
{
hours = 0;
minutes = 0;
}
Time(int h, int m)
{
hours = h;
minutes = m;
}
return result;
}
void display()
{
cout << "Time: " << hours << " hours and " << minutes << " minutes" <<
endl;
}
};
int main()
{
clrscr();
Time t1(5, 10);
Time t2(4, 18);
Time sum = t1 + t2;
t1.display();
t2.display();
sum.display();
getch();
}
Output :
Time : 5 hours and 10 minutes
Time : 4 hours and 18 minutes
Time : 9 hours and 28 minutes
public:
class Title
{
char bookTitle[100];
public:
Title(const char* title)
{
strcpy(bookTitle, title);
}
public:
Book(const char* author, const char* title, int year)
: Author(author), Title(title), publicationYear(year)
{}
void display()
{
cout << "Title: " << getBookTitle() << endl;
cout << "Author: " << getAuthorName() << endl;
cout << "Publication Year: " << getPublicationYear() << endl;
}
};
int main()
{
clrscr();
Book book("J.K. Rowling", "Harry Potter and the Sorcerer's Stone", 1997);
getch();
return 0;
}
Output :
Book Information ::
Title : Harry Potter and the Sorcerer’s Stone
Author : J.K. Rowling
Publication Year : 1997
Q.13 Write a program in C++ to find MAX and MIN element row wisefrom
matrix using operator overloading > and < operator.
#include <iostream.h>
#include <conio.h>
class Matrix
{
private:
int data[3][3];
public:
Matrix(const int input_data[3][3])
{
for (int i = 0; i < 3; ++i)
{
int matrix_data[3][3] =
{
{5, 12, 8},
{3, 9, 6},
{15, 7, 10}
};
Matrix matrix(matrix_data);
cout << "Maximum element in matrix: " << (matrix > matrix) << endl;
cout << "Minimum element in matrix: " << (matrix < matrix) << endl;
getch();
}
Output :
Maximum element in matrix : 15
Minimum element in matrix : 3
public:
void createTour()
{
clrscr();
cout << "Enter Tour Code: ";
cin >> tourCode;
cin.ignore();
cout << "Enter Tour Name: ";
cin.getline(tourName, 50);
cout << "Enter Tour Cost: ";
cin >> tourCost;
}
void displayTour()
{
clrscr();
cout << "Tour Code: " << tourCode << endl;
cout << "Tour Name: " << tourName << endl;
cout << "Tour Cost: " << tourCost << endl;
getch();
}
void increaseCost()
{
tourCost *= 1.1;
void decreaseCost()
{
tourCost *= 0.9;
}
void changeName()
{
clrscr();
cout << "Enter new Tour Name: ";
cin.ignore();
cin.getline(tourName, 50);
}
void changeCost()
{
clrscr();
cout << "Enter new Tour Cost: ";
cin >> tourCost;
}
void saveToFile()
{
ofstream outFile("tours.txt", ios::app);
if (outFile)
{
outFile << tourCode << "," << tourName << "," << tourCost << endl;
cout << "Tour data saved to file." << endl;
outFile.close();
}
else
{
cout << "Error: Unable to open file." << endl;
}
getch();
}
void retrieveFromFile()
{
ifstream inFile("tours.txt");
if (inFile)
{
char line[100];
while (inFile.getline(line, 100))
{
char *token = strtok(line, ",");
strcpy(tourCode, token);
token = strtok(NULL, ",");
strcpy(tourName, token);
token = strtok(NULL, ",");
tourCost = atof(token);
displayTour();
}
inFile.close();
}
else
{
cout << "Error: Unable to open file." << endl;
}
getch();
}
};
int main()
{
clrscr();
Tour tour;
int choice;
do {
clrscr();
cout << "\nMenu:\n";
cout << "1. Create Tour\n";
cout << "2. Display Tour\n";
cout << "3. Increase Tour Cost\n";
cout << "4. Decrease Tour Cost\n";
cout << "5. Change Tour Name\n";
cout << "6. Change Tour Cost\n";
cout << "7. Save to File\n";
switch (choice) {
case 1:
tour.createTour();
break;
case 2:
tour.displayTour();
break;
case 3:
tour.increaseCost();
break;
case 4:
tour.decreaseCost();
break;
case 5:
tour.changeName();
break;
case 6:
tour.changeCost();
break;
case 7:
tour.saveToFile();
break;
case 8:
tour.retrieveFromFile();
break;
case 9:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please enter a valid option." << endl;
}
getch();
}
Output :
Menu :
1. Create Tour
2. Display Tour
3. Increase Tour Cost
4. Decrease Tour Cost
5. Change Tour Name
6. Change Tour Cost
7. Save to File
8. Retrive from File
9. Exit
Enter your choice : 1
-------------------------------------------------------------------------------------------------------
Enter Tour Code : 101
public:
Month() : monthNumber(1)
{
}
void read()
{ do{
cout<< "Enter month number: ";
cin>> monthNumber;
}
while(monthNumber < 1 || monthNumber > 12);
}
void display()
{