0% found this document useful (0 votes)
15 views16 pages

Oops 3

Object Oriented Programming Notes

Uploaded by

Abcd Efgh
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)
15 views16 pages

Oops 3

Object Oriented Programming Notes

Uploaded by

Abcd Efgh
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/ 16

Program-15

Aim:- Write a program to define the function template for calculating the square
of given numbers with different data types.

Program :-

#include <iostream>
using namespace std;

template <class T>


T square(T x)
{
return x * x;
}

int main()
{
int n1;
float n2;
double n3;
cout << "Enter the values of n1(int), n2(float) and n3(double): ";
cin >> n1 >> n2 >> n3;
cout << "n1^2 = " << square<int>(n1) << endl;
cout << "n2^2 = " << square<float>(n2) << endl;
cout << "n3^2 = " << square<double>(n3) << endl;
return 0;
}
Program-16

Aim:- Create a base class basic_info with data members name, roll no, sex and two
member functions getdata and display. Derive a class physical_fit from basic_info
which has data members height and weight and member functions getdata and
display. Display all the information using object of derived class.

Program :-

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


using namespace std;

class basic_info
{
string name;
int roll_no;
char sex;

public:
void getdata()
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll no: ";
cin >> roll_no;
cout << "Enter sex: ";
cin >> sex;
}
void display()
{
cout << "Name: " << name << endl;
cout << "Roll No: " << roll_no << endl;
cout << "Sex: " << sex << endl;
}
};

class physical_fit : public basic_info


{
float height, weight;

public:
void get_data()
{
cout << "Enter height: ";
cin >> height;
cout << "Enter weight: ";

cin >> weight;


}
void Display()
{
cout << "Height: " << height << endl;
cout << "Weight: " << weight << endl;
}
};

int main()
{
physical_fit A;
A.getdata();
A.get_data();
cout << "The details of the person are:\n";
A.display();
A.Display();
return 0;
}
Program-17

Aim:- Create class first with data members book no, book name and member
functions getdata and putdata. Create a class second with data members author
name, publisher and member functions getdata and showdata. Derive a class third
from first and second with data members no of pages and year of publication.
Display all this information using array of objects of third class.

Program :-
#include <iostream>
#include <string.h>
using namespace std;

class first
{
int book_no;
string book_name;

public:
void get_data()
{
cout << "Enter book number: ";
cin >> book_no;
cout << "Enter book name: ";
cin >> book_name;
}
void put_data()
{
cout << "Book number: " << book_no << endl;
cout << "Book name: " << book_name << endl;
}
};

class second
{
string author_name, publisher;

public:
void get_data_2()
{
cout << "Enter author name: ";
cin >> author_name;
cout << "Enter publisher: ";
cin >> publisher;
}
void show_data()
{

cout << "Author Name: " << author_name << endl;


cout << "Publisher: " << publisher << endl;
}
};

class third : public first, public second


{
int no_of_pages, year;

public:
third()
{
get_data();
get_data_2();
cout << "Enter number of pages: ";
cin >> no_of_pages;
cout << "Enter year of publication: ";
cin >> year;
}
void display()
{
put_data();
show_data();
cout << "Number of pages: " << no_of_pages << endl;
cout << "Year of publication: " << year << endl;
}
};

int main()
{
third books[] = {third(), third()};
cout << "\n\n";
cout << "Details of the first book are: \n";
books[0].display();
cout << "Details of the second book are: \n";
books[1].display();
return 0;
}
Program-18

Aim:- Design three classes STUDENT, EXAM and RESULT. The STUDENT class has
data members such as roll no, name. Create a class EXAM by inheriting the
STUDENT class. The EXAM class adds data members representing the marks scored
in six subjects. Derive the RESULT from the EXAM class and has its own data
members such as total marks. Write a program to model this relationship.

Program :-

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

class STUDENT
{
string name;
int roll_no;

public:
void get_data()
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> roll_no;
}
void put_data()
{
cout << "Name: " << name << endl;
cout << "Roll No: " << roll_no << endl;
}
};

class EXAM : public STUDENT


{
float marks[6];

public:
int sum = 0;
void get_marks()
{
for (int i = 0; i < 6; i++)
{
cout << "Enter subject " << i + 1 << " marks: ";
cin >> marks[i];
sum += marks[i];
}
}
void show_marks()
{
for (int i = 0; i < 6; i++)
{
cout << "Subject " << i + 1 << " marks: " << marks[i] << endl;
}
}
};

class RESULT : public EXAM


{
int total_marks;

public:
void get_total()
{
total_marks = sum;
}
void display()
{
put_data();
show_marks();
cout << "Total marks: " << total_marks << endl;
}
};

int main()
{
RESULT A;
A.get_data();
A.get_marks();
A.get_total();
cout << "\n\n";
cout << "The details of the student are: \n";
A.display();
return 0;
}
Program-19

Aim:- Create a base class called SHAPE. Use this class to store two double type
values. Derive two specific classes called TRIANGLE and RECTANGLE from the base
class. Add to the base class, a member function getdata to initialize base class data
members and another member function to display to compute and display the area
of figures. Make display a virtual function and redefine this function in the derived
classes to suit their requirements. Using these three classes design a program that
will accept driven of a TRIANGLE or RECTANGLE interactively and display the area.

Program :-

#include <iostream>
using namespace std;

class SHAPE
{
double para1, para2;

public:
void get_para()
{
cin >> para1 >> para2;
}
double put_para1()
{
return para1;
}
double put_para2()
{
return para2;
}
virtual void display() {}
};

class TRIANGLE : public SHAPE


{
public:
void get_data()
{
cout << "Enter the base and height of the triangle: ";
get_para();
}
void display()
{
double area = 0.5 * put_para1() * put_para2();
cout << "Area of the triangle: " << area << endl;
}
};

class RECTANGLE : public SHAPE


{
public:
void get_data()
{
cout << "Enter the lenght and breadth of the rectangle: ";
get_para();
}
void display()
{
double area = put_para1() * put_para2();
cout << "Area of the rectangle: " << area << endl;
}
};

int main()
{
TRIANGLE t;
RECTANGLE r;
bool running = true;
while (running)
{
int choice;
cout << "\n\n1. Triangle\n2. Rectangle\n3. Exit\nEnter you choice: ";
cin >> choice;
if (choice == 1)
{
t.get_data();
t.display();
}
else if (choice == 2)
{
r.get_data();
r.display();
}
else if (choice == 3)
{
running = false;
}
else
{
cout << "Enter a valid choice! Try again!\n";
}
}
return 0;
}
Program-20

Aim:- Write a program to define the function template for swapping two items of
various datatypes such as integers, float and characters.

Program :-

#include <iostream>
using namespace std;

template <class T>


T Swap(T x, T y)
{
T temp = x;
x = y;
y = temp;
cout << "Variables after swapping the values: \n";
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}

int main()
{
cout << "Enter two integers: ";
int i1, i2;
cin >> i1 >> i2;
Swap<int>(i1, i2);
cout << "Enter two float numbers: ";
float f1, f2;
cin >> f1 >> f2;
Swap<float>(f1, f2);
cout << "Enter two double numbers: ";
double d1, d2;
cin >> d1 >> d2;
Swap<double>(d1, d2);
cout << "Enter two chars: ";
char c1, c2;
cin >> c1 >> c2;
Swap<char>(c1, c2);
return 0;
}
Program-21

Aim:- Write a program to illustrate how to define and declare a class template for
reading two items from the keyboard and to find their sum.

Program :-

#include <iostream>
using namespace std;

template <typename T>


class TEMP
{
public:
T *ptr;
TEMP(T x, T y)
{
T sum = x + y;
ptr = &sum;
}
void print()
{
cout << "The sum of the vlaues is " << *ptr << endl;
}
};

int main()
{
cout << "Enter two integers: ";
int i1, i2;
cin >> i1 >> i2;
TEMP<int> i(i1, i2);
i.print();
cout << "Enter two float numbers: ";
float f1, f2;
cin >> f1 >> f2;
TEMP<int> f(f1, f2);
f.print();
cout << "Enter two double numbers: ";
double d1, d2;
cin >> d1 >> d2;
TEMP<int> d(d1, d2);
d.print();

cout << "Enter two chars: ";


char c1, c2;
cin >> c1 >> c2;
TEMP<int> c(c1, c2);
c.print();
return 0;
}

You might also like