0% found this document useful (0 votes)
32 views44 pages

OOPS

The document contains 5 questions related to C++ programming concepts like classes, inheritance, polymorphism etc. Each question provides code to create classes for different shapes or entities and demonstrate concepts like encapsulation, data hiding, inheritance etc. The code takes input from the user, performs calculations like area of shapes and displays the output.

Uploaded by

Raj Patel
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)
32 views44 pages

OOPS

The document contains 5 questions related to C++ programming concepts like classes, inheritance, polymorphism etc. Each question provides code to create classes for different shapes or entities and demonstrate concepts like encapsulation, data hiding, inheritance etc. The code takes input from the user, performs calculations like area of shapes and displays the output.

Uploaded by

Raj Patel
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
You are on page 1/ 44

Patel Tanisha Ashokbhai Div-D(20)

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;

OOPs and Data Structures(OOPS) 1


Patel Tanisha Ashokbhai Div-D(20)

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;

OOPs and Data Structures(OOPS) 2


Patel Tanisha Ashokbhai Div-D(20)

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

OOPs and Data Structures(OOPS) 3


Patel Tanisha Ashokbhai Div-D(20)

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()

OOPs and Data Structures(OOPS) 4


Patel Tanisha Ashokbhai Div-D(20)

{
return temperature;
}

void setTemperature(double temp)


{
temperature = temp;
}

void convertToFahrenheit()
{
temperature = (temperature * 9 / 5) + 32;
}
};
class Fahrenheit
{ private:
double temperature;

public:
Fahrenheit(double temp) : temperature(temp)
{
}

double getTemperature()
{
return temperature;

OOPs and Data Structures(OOPS) 5


Patel Tanisha Ashokbhai Div-D(20)

}
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();

OOPs and Data Structures(OOPS) 6


Patel Tanisha Ashokbhai Div-D(20)

cout << "Equivalent temperature in Fahrenheit: " << fahrenheitTemp<< endl;


getch();
}

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;

OOPs and Data Structures(OOPS) 7


Patel Tanisha Ashokbhai Div-D(20)

}
};
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:

OOPs and Data Structures(OOPS) 8


Patel Tanisha Ashokbhai Div-D(20)

Result(int roll, int marks1, int marks2) : Student(roll), ClassTest(marks1,


marks2)
{
totalMarks = marks1 + marks2;
}

int getTotalMarks()
{
return totalMarks;
}
};
int main()
{ clrscr();
int roll_no, marks1, marks2;

cout << "Enter roll number: "; cin >> roll_no;


cout << "Enter marks for subject 1: "; cin >> marks1;
cout << "Enter marks for subject 2: "; cin >> marks2;

Result resultObj(roll_no, marks1, marks2);

cout << "\nRoll Number: " << resultObj.getRollNo() << endl;


cout << "Subject 1 Marks: " << resultObj.getSubject1Marks() << endl;
cout << "Subject 2 Marks: " << resultObj.getSubject2Marks() << endl;
cout << "Total Marks: " << resultObj.getTotalMarks() << endl;
getch();
}

OOPs and Data Structures(OOPS) 9


Patel Tanisha Ashokbhai Div-D(20)

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)
{

OOPs and Data Structures(OOPS) 10


Patel Tanisha Ashokbhai Div-D(20)

area = PI * radius * radius;


}

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 :

OOPs and Data Structures(OOPS) 11


Patel Tanisha Ashokbhai Div-D(20)

Enter the radius of the circle : 5

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>

class Shape { protected:


float length; float breadth;
float radius;

public:
virtual void calculateArea() = 0;
virtual void displayArea() = 0;
};

class Circle : public Shape {


public:
void calculateArea() {
cout << "Enter the radius of the circle: ";
cin >> radius;

OOPs and Data Structures(OOPS) 12


Patel Tanisha Ashokbhai Div-D(20)

void displayArea() {
float area = 3.1415 * radius * radius;
cout << "Area of the circle: " << area << endl;
}
};

class Rectangle : public Shape { public:


void calculateArea() {
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the breadth of the rectangle: ";
cin >> breadth;
}

void displayArea() {
float area = length * breadth;
cout << "Area of the rectangle: " << area << endl;
}
};

int main() {
clrscr();

Shape* shape;

OOPs and Data Structures(OOPS) 13


Patel Tanisha Ashokbhai Div-D(20)

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

Q.6 Write a program to overload + and == operator for string manipulation.


#include <iostream.h>
#include <conio.h>
#include <string.h>

OOPs and Data Structures(OOPS) 14


Patel Tanisha Ashokbhai Div-D(20)

class MyString {
private:
char* str;

public:
MyString(const char* s) {
str = new char[strlen(s) + 1];
strcpy(str, s);
}

MyString(const MyString& other) {


str = new char[strlen(other.str) + 1];
strcpy(str, other.str);
}

~MyString() {
delete[] str;
}

MyString operator+(const MyString& other) {


char* temp = new char[strlen(str) + strlen(other.str) + 1];
strcpy(temp, str);
strcat(temp, other.str);
MyString result(temp);
delete[] temp;
return result;

OOPs and Data Structures(OOPS) 15


Patel Tanisha Ashokbhai Div-D(20)

void display() {
cout << str << endl;
}

const char* c_str() const {


return str;
}
};

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 {

OOPs and Data Structures(OOPS) 16


Patel Tanisha Ashokbhai Div-D(20)

cout <<endl<< "s1 and s2 are not equal." << endl;


}

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

s1 and s2 are not equal.


s1 and s3 are equal.

Q.7 Write a program to overload >> and << operators.


#include<iostream.h> #include<conio.h>
class MyClass
{
int value;
public:
MyClass(int val = 0) : value(val)

OOPs and Data Structures(OOPS) 17


Patel Tanisha Ashokbhai Div-D(20)

{
}

friend istream& operator>>(istream& input, MyClass& obj)


{
input >> obj.value;
return input;
}

friend ostream& operator<<(ostream& output, const MyClass& obj)


{
output << obj.value;
return output;
}
};
int main()
{
clrscr();
MyClass obj;
cout << "Enter a value: ";
cin >> obj;
cout << "Value entered: " << obj << endl;
getch();
}
Output :
Enter a value : 10

OOPs and Data Structures(OOPS) 18


Patel Tanisha Ashokbhai Div-D(20)

Value entered : 10

Q.8 Write a program to overload the == and += operator (string object).

➢ Ans:-
#include <iostream.h>
#include <string.h>
#include<conio.h>
// to concatinate string
class AddString {

public:

char s1[25], s2[25];


AddString(char str1[], char str2[])
{
strcpy(this->s1, str1);
strcpy(this->s2, str2);
}
void operator+()
{
cout << "\nConcatenation: " << strcat(s1, s2);
}
};

// to compaire string

OOPs and Data Structures(OOPS) 19


Patel Tanisha Ashokbhai Div-D(20)

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;
}

int operator<=(CompareString s3)


{
if (strlen(str) <= strlen(s3.str))
return 1;
else
return 0;
}
};

void compare(CompareString s1, CompareString s2)

OOPs and Data Structures(OOPS) 20


Patel Tanisha Ashokbhai Div-D(20)

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()
{

char str1[] = "Geeks";


char str2[] = "ForGeeks";

CompareString s1(str1);
CompareString s2(str2);

cout << "Comparing \"" << s1.str << "\" and \""
<< s2.str << "\"" << endl;

compare(s1, s2);
}

OOPs and Data Structures(OOPS) 21


Patel Tanisha Ashokbhai Div-D(20)

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";

AddString a1(str1, str2);

+a1;
getch();
}

OOPs and Data Structures(OOPS) 22


Patel Tanisha Ashokbhai Div-D(20)

Output:
Comparing “Geeks” and “Geeks”
Geeks is not equal to Geeks.

Comparing “Geeks” and “Geeks”


Geeks is equal to Geeks.

Concatenation: Patel Tanu

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;

OOPs and Data Structures(OOPS) 23


Patel Tanisha Ashokbhai Div-D(20)

}
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

OOPs and Data Structures(OOPS) 24


Patel Tanisha Ashokbhai Div-D(20)

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:

OOPs and Data Structures(OOPS) 25


Patel Tanisha Ashokbhai Div-D(20)

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();

OOPs and Data Structures(OOPS) 26


Patel Tanisha Ashokbhai Div-D(20)

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)
{

OOPs and Data Structures(OOPS) 27


Patel Tanisha Ashokbhai Div-D(20)

hours = h;
minutes = m;
}

Time operator+(const Time& other)


{
Time result;
result.hours = hours + other.hours;
result.minutes = minutes + other.minutes;

if (result.minutes >= 60)


{
result.minutes -= 60;
result.hours++;
}

return result;
}

void display()
{
cout << "Time: " << hours << " hours and " << minutes << " minutes" <<
endl;
}
};
int main()
{

OOPs and Data Structures(OOPS) 28


Patel Tanisha Ashokbhai Div-D(20)

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

Q.12 C++ program to read and print books information.Using multiple


inheritance.
#include <iostream.h>
#include <string.h>
#include<conio.h>
class Author
{
char authorName[50];

public:

OOPs and Data Structures(OOPS) 29


Patel Tanisha Ashokbhai Div-D(20)

Author(const char* name)


{
strcpy(authorName, name);
}

const char* getAuthorName() const


{
return authorName;
}
};

class Title
{
char bookTitle[100];

public:
Title(const char* title)
{
strcpy(bookTitle, title);
}

const char* getBookTitle() const


{
return bookTitle;
}
};

OOPs and Data Structures(OOPS) 30


Patel Tanisha Ashokbhai Div-D(20)

class Book : public Author, public Title


{
int publicationYear;

public:
Book(const char* author, const char* title, int year)
: Author(author), Title(title), publicationYear(year)
{}

int getPublicationYear() const


{
return publicationYear;
}

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);

OOPs and Data Structures(OOPS) 31


Patel Tanisha Ashokbhai Div-D(20)

cout << "Book Information:" << endl;


book.display();

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)
{

OOPs and Data Structures(OOPS) 32


Patel Tanisha Ashokbhai Div-D(20)

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


{
data[i][j] = input_data[i][j];
}
}
}

int operator>(const Matrix& other) const


{
int max_element = data[0][0];
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (data[i][j] > max_element)
{
max_element = data[i][j];
}
}
}
return max_element;
}

int operator<(const Matrix& other) const


{
int min_element = data[0][0];

OOPs and Data Structures(OOPS) 33


Patel Tanisha Ashokbhai Div-D(20)

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


{
for (int j = 0; j < 3; ++j)
{
if (data[i][j] < min_element)
{
min_element = data[i][j];
}
}
}
return min_element;
}
};
int main()
{
clrscr();

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;

OOPs and Data Structures(OOPS) 34


Patel Tanisha Ashokbhai Div-D(20)

cout << "Minimum element in matrix: " << (matrix < matrix) << endl;
getch();
}
Output :
Maximum element in matrix : 15
Minimum element in matrix : 3

Q.14 Write a C++ menu driven program to


1.Create class “Tour” containing member variables like Tour code,
Tour_name, and Tour_cost.
2.Overload * and / operator to increase tour cost by 10% and decrease tour
cost by 10% respectively.
3.Provide facility to change Tour_name and Tour_cost.
4.Save data into file.
5.Retrieve data from file.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
class Tour
{
private:
char tourCode[20];
char tourName[50];
double tourCost;

OOPs and Data Structures(OOPS) 35


Patel Tanisha Ashokbhai Div-D(20)

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;

OOPs and Data Structures(OOPS) 36


Patel Tanisha Ashokbhai Div-D(20)

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)

OOPs and Data Structures(OOPS) 37


Patel Tanisha Ashokbhai Div-D(20)

{
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);

OOPs and Data Structures(OOPS) 38


Patel Tanisha Ashokbhai Div-D(20)

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";

OOPs and Data Structures(OOPS) 39


Patel Tanisha Ashokbhai Div-D(20)

cout << "8. Retrieve from File\n";


cout << "9. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

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();

OOPs and Data Structures(OOPS) 40


Patel Tanisha Ashokbhai Div-D(20)

break;
case 8:
tour.retrieveFromFile();
break;
case 9:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please enter a valid option." << endl;
}

} while (choice != 9);

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

OOPs and Data Structures(OOPS) 41


Patel Tanisha Ashokbhai Div-D(20)

Enter Tour Name : SSurat To Dubai


Enter Tour Cost : 100000
Q.15 Construct a class Month with data members as month number. Write
C++ program with overloaded operator “++”, which increments the month
Number by one.Write a member function read() and display() to read a
number of month and print a Number of month.
#include<iostream.h>
#include<conio.h>
class Month
{
private:
int monthNumber;

public:
Month() : monthNumber(1)
{
}

void read()
{ do{
cout<< "Enter month number: ";
cin>> monthNumber;
}
while(monthNumber < 1 || monthNumber > 12);
}
void display()
{

OOPs and Data Structures(OOPS) 42


Patel Tanisha Ashokbhai Div-D(20)

cout<<"Next Month number is: "<<monthNumber<<endl;


}

// Overloaded prefix increment operator


Month& operator++()
{
monthNumber=(monthNumber % 12)+1;
return *this;
}
};
int main()
{
clrscr();
Month m; m.read();
++m; // Increment the month
m.display();
getch();
}
Output :
Enter month number : 12
Next month number is : 1

OOPs and Data Structures(OOPS) 43


Patel Tanisha Ashokbhai Div-D(20)

OOPs and Data Structures(OOPS) 44

You might also like