Professional Documents
Culture Documents
INSTRUCTIONS TO STUDENTS
Before entering the lab the student should carry the following things (MANDATORY)
2. Class notes
3. Lab Manual
4. Lab Record
Student must sign in and sign out in the register provided when attending the lab session
without fail.
Come to the laboratory in time. Students, who are late more than 15 min., will not be
allowed to attend the lab.
Students need to maintain 100% attendance in lab if not a strict action will be taken.
Refer to the lab staff if you need any help in using the lab.
Read the Manual carefully before coming to the laboratory and be sure about what you are
supposed to do.
Copy all the programs to observation which are taught in class before attending the lab
session.
Students are not supposed to use floppy disks, pen drives without permission of lab- in
charge.
Lab#1
Intro to Pointers
Program:
Write a program that inputs a number in an integer variable. It stores the address of the variable in a
pointer and then displays the values and address of the variable.
Code:
#include<iostream>
using namespace std;
int main(){
int num;
int *ptr = #
cout<<"Enter the num: ";
cin>>num;
cout<<"Value : "<<*ptr<<endl;
cout<<"Address: "<<ptr<<endl;
return 0;
}
Output:
Program:
Write a program to input five integers in an array and display them using a pointer.
Code:
#include<iostream>
using namespace std;
int main(){
int num[5] = {2,9,4,9,6};
int *ptr =num;
cout<<"Array Elements are: "<<endl;
for(int i=0; i<5; i++){
cout<<*(ptr+i)<<"\t";
}
cout<<endl;
return 0;
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Program:
Write a program that inputs five floating point values in an array and displays the values in reverse order.
Code:
#include<iostream>
using namespace std;
int main(){
float num[5] = {2.9, 2.8, 3.7, 2.6, 1.7};
float *ptr;
ptr = &num[5];
cout<<"Array elements are"<<endl;
for(int i=1; i<6; i++){
cout<<*(ptr-i)<<"\t";
}
cout<<endl;
return 0;
}
Output:
Program:
Write a program that inputs a string values from the user and displays it using pointer.
Code:
#include<iostream>
using namespace std;
int main(){
char name[20], *ptr;
cout<<"Enter your name: ";
cin.get(name, 20);
ptr = name;
cout<<"Your Name is : "<<ptr<<endl;
}
Output:
Program:
Write a program that declares and initialize a string. It inputs a character from the user and searches the
character in the array.
Code:
#include <iostream>
using namespace std;
int main(){
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Program:
Write a program that uses new operator to create an integer, inputs values in it and then displays that
values.
Code:
#include <iostream>
using namespace std;
int main(){
int *ptr;
ptr = new int;
cout<<"Enter an integer: ";cin>>*ptr;
cout<<"You Entered: "<<*ptr<<endl;
cout<<"It is stored at: "<<ptr<<endl;
return 0;
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Lab 2
Introduction to Functions
Program:
Write a program that displays a message ‘Programming makes life interesting’ on screen using
functions.
Code:
#include<iostream>
using namespace std;
void show();
int main(){
show();
return 0;
}
void show(){
cout<<"Programming makes life interesting!!"<<endl;
}
Output:
Program:
Write a program that inputs a number in main function and passes the number to a function.
The function displays table of that number.
Code:
#include<iostream>
using namespace std;
void table(int);
int main(){
int num;
cout<<"Enter a table number: ";cin>>num;
table(num);
return 0;
}
void table(int n){
for(int i=1;i<=10;i++){
cout<<n<<" * "<<i<<" = "<<(i*n)<<endl;
}
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
Program:
Write a program that inputs two numbers and one arithmetic operator in main function and
passes them to a function. The function applies arithmetic operation on two numbers on the
basis of the operator entered by user using switch statement.
Code:
#include<iostream>
using namespace std;
void calc(float,float,char);
int main(){
float a,b;
char op;
cout<<"Enter a first number: ";cin>>a;
cout<<"Enter a second number: ";cin>>b;
cout<<"Enter a operator: ";cin>>op;
calc(a,b,op);
return 0;
}
void calc(float x, float y, char op){
switch(op){
case '+':
cout<<x<<" + "<<y<<" = "<<x+y<<endl;
break;
case '-':
cout<<x<<" - "<<y<<" = "<<x-y<<endl;
break;
case '*':
cout<<x<<" * "<<y<<" = "<<x*y<<endl;
break;
case '/':
cout<<x<<" / "<<y<<" = "<<x/y<<endl;
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
break;
default:
cout<<"Invalid input"<<endl;
}
}
Output:
Program:
Write a program that inputs a number in main function and passes the number to a function.
The function displays the factorial of that number.
Code:
#include<iostream>
using namespace std;
void fact(int);
int main(){
int num;
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Program:
Write a program that inputs two integers. It passes first integer to a function that calculates and
returns its square. It passes second integer to another function that calculates and returns its
cube. The main function adds both returned values and displays the result.
Code:
#include<iostream>
using namespace std;
int sq(int);
int cb(int);
int main(){
int num1,num2,sum;
LAB 3
Taking input through member functions
Program:
Write a program that declares a class with one integer data member and two member functions in() and
out() to input and output data in data member.
Code:
#include<iostream>
using namespace std;
class Hussain{
private:
int x;
public:
void in(){
cout<<"Enter a number: ";
cin>>x;
}
void out(){
cout<<"You Entered: "<<x<<endl;
}
};
int main(){
Hussain obj;
obj.in();
obj.out();
return 0;
}
Output:
Program:
Write a program that declares a class with name, age, address as data member and two member functions
in() and out() to input and output data in data member.
Code:
#include<iostream>
using namespace std;
class Hussain{
private:
string name, address;
int age;
public:
void in(){
getline(cin, address);
cout<<"Enter your age: ";
cin>>age;
}
void out(){
cout<<"Name: "<<name<<endl;
cout<<"Address: "<<address<<endl;
cout<<"Age: "<<age<<endl;
}
};
int main(){
Hussain obj;
obj.in();
obj.out();
return 0;
}
Output:
Program:
Write a class marks with three data members to store three marks. Write three member functions in() to
input marks, sum() to calculate sum and return sum and avg() to calculate and return average of marks.
Code:
#include<iostream>
using namespace std;
class Hussain{
private:
int a,b,c,s;
float avg;
public:
void in(int a1, int a2, int a3){
a = a1;
b = a2;
c = a3;
}
int sum(){
s = a+b+c;
return s;
}
float average(){
return s/3.0;
}
};
int main(){
Hussain obj;
int a,b,c;
cout<<"Enter three numbers: ";
cin>>a>>b>>c;
obj.in(a,b,c);
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Program:
Write a class Circle with one data member radius. Write three member functions get_radius to set value
with parameter value, area() to calculate and display area of circle and circum() to calculate and display
circumference of circle.
Code:
#include<iostream>
using namespace std;
class Circle{
private:
float r;
public:
void in(float r1){
r = r1;
}
void area(){
cout<<"Area of circle is "<<(3.14*r*r)<<endl;
}
void circum(){
cout<<"Circumference of circle is "<<(2*3.14*r)<<endl;
}
};
int main(){
Circle obj;
float rad;
cout<<"Enter radius: ";
cin>>rad;
obj.in(rad);
obj.area();
obj.circum();
return 0;
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
Program:
Write a class Book with three data members BookID, Pages and Price. It also contains the following
member functions.
The get() function is used to input values.
The show() function is used to display values.
The set() function used to set values of data members using parameters.
The getPrice() function is used to return the value of price.
The program should create two objects of the class and input values for these objects. The program should
display the details of most costly book.
Code:
#include <iostream>
using namespace std;
class book{
private:
int bookid;
int pages;
int price;
public:
void get (){
cout << "Please Enter the ID of the book: ";
cin >> bookid;
cout << "Please Enter the pages of the book: ";
cin >> pages;
cout << "Please Enter the price of the book: ";
cin >> price;
}
int main ()
{
book b1, b2;
b1.get();
b2.set(2, 320, 150.34);
cout << "Details of costly book is: ";
if(b1.getPrice()>b2.getPrice()){
b1.show();
}else{
b2.show();
}
}
Output:
Program:
Write a class Result that contains roll no, name and marks of three subjects. The marks are stored in an
array of integers. The class also contains the following functions.
The input() function is used to input the values in data members.
The show() function is used to display the value of data member.
The total() function returns the total marks of a student.
The avg() function returns the average marks of a student.
The program should create an object of class and call the member functions.
Code:
#include <iostream>
using namespace std;
class result{
private:
int bookid, marks[3];
char name[50];
public:
void get (){
cout << "Please Enter rno: ";
cin >> bookid;
cout << "Please Enter name: ";
cin >> name;
for(int i=0; i<3;i++){
cout<<"Enter Marks["<<i<<"]: ";
cin>>marks[i];
}
}
void show ()
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
{
cout <<"Roll No: " << bookid << endl;
cout <<"Name " << name << endl;
for(int i=0; i<3;i++){
cout<<"Enter Marks["<<i<<"]: "<<marks[i]<<endl;
}
}
int total(){
int t=0;
for(int i=0; i<3;i++){
t = t+marks[i];
}
return t;
}
int avg(){
int t=0;
for(int i=0; i<3;i++){
t = t+marks[i];
}
return t/3.0;
}
};
int main ()
{
result r;
r.get();
r.show();
cout<<"Total marks: "<<r.total()<<endl;
cout<<"Average marks: "<<r.avg()<<endl;
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
LAB 4
Define member functions outside class
Program:
Write a class Array that contains array of integers to store five values. It also contains the following
member functions:
The fill() function is used to fill the array with values from the user.
The display() function is used to display the values of array.
The max() function shows the maximum values in the array.
The min() functions shows the minimum values in the array.
All member functions should be defined outside the class.
Code:
#include <iostream>
using namespace std;
class array
{
private:
int arr[5];
public:
void fill ();
void display ();
void max ();
void min ();
};
int main (){
array a1;
a1.fill();
a1.display();
a1.max();
a1.min();
}
void array :: fill (){
for (int i = 0; i < 5; i++)
{
cout << "Enter any Numbers " << i + 1 << ": ";
cin >> arr[i];
}
}
void array :: display (){
for (int i = 0; i < 5; i++)
{
cout << "The Number of " << i + 1 << ": " << arr[i] <<
endl;
}
}
void array :: max (){
int maxi = arr[0];
for (int i = 0; i < 5; i++)
{
if (arr[i] > maxi)
{
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
maxi = arr[i];
}
}
cout << "Maximum = " << maxi << endl;
}
void array :: min (){
int mini = arr[0];
for (int i = 0; i < 5; i++)
{
if (arr[i] < mini)
{
mini = arr[i];
}
}
cout << "Minimum = " << mini << endl;
}
Output:
Program:
Write a class Array that contains array of integers to store five values. It also contains the following
member functions:
The fill() function is used to fill the array with values from the user.
The display() function is used to display the values of array.
The asc() function which sort the array in ascending order and display on screen.
All member functions should be defined outside the class.
Code:
#include <iostream>
using namespace std;
class array
{
private:
int arr[5];
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
public:
void fill ();
void display ();
void asc ();
};
int main (){
array a1;
a1.fill();
a1.display();
a1.asc();
}
void array :: fill (){
for (int i = 0; i < 5; i++)
{
cout << "Enter Number["<<i<<"]: ";
cin >> arr[i];
}
}
void array :: display (){
cout << "Your Entered: "<<endl;
for (int i = 0; i < 5; i++)
{
cout << "Number["<<i<<"] = " << arr[i] << endl;
}
}
Program:
Write a class that contains a function to input number to calculate the factorial of a number. And a
function to calculate and return factorial of that number. All member functions should be defined outside
the class.
Code:
#include <iostream>
using namespace std;
class factorial
{
private:
int n;
public:
void in();
void f ();
};
int main (){
factorial m;
m.in();
m.f();
}
void factorial :: in (){
cout<<"Enter a number: ";
cin>>n;
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Program:
Write a class which contains a function to input marks of 10 students through parameters. It also declare a
function to calculate the average and a function to display the grade of student.
Code:
#include <iostream>
using namespace std;
class marks
{
private:
int a,b,c,d,e,f,g,h,i,j;
public:
void fill (int a1,int a2,int a3,int a4,int a5,int a6,int
a7,int a8,int a9,int a10);
void display ();
void avg ();
void grade();
};
int main (){
marks m;
m.fill(21,32,43,12,54,65,76,23,76,23);
m.avg();
m.grade();
}
void marks :: fill (int a1,int a2,int a3,int a4,int a5,int a6,int
a7,int a8,int a9,int a10){
a = a1;
b = a2;
c = a3;
d = a4;
e = a5;
f = a6;
g = a7;
h = a8;
i = a9;
j = a10;
}
void marks :: avg (){
cout<<"Average: "<<(a+b+c+d+e+f+g+h+i+j)/10.0<<endl;
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
LAB 5
Constructors
Program:
Write a class that displays a simple message on the screen whenever an object of that class is created.
Code:
#include <iostream>
using namespace std;
class constr{
public:
constr ()
{
cout << "Object is created!"<<endl;
}
};
int main ()
{
constr p,q,e,r;
}
Output:
Program:
Write a class that contains two integer data members which are initialized to 100 and 200 when an object
is created. It has a member function sum that display sum of the data members.
Code:
#include <iostream>
using namespace std;
class data{
private :
int numb1, numb2;
public :
data ()
{
numb1 = 100;
numb2 = 200;
}
void add()
{
int sum = numb1 + numb2;
cout << "Sum is " << sum << endl;
}
};
int main ()
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
{
data d1;
d1.add();
}
Output:
Program:
Write a class that contains two integer data members which are initialized to 100 and 200 when an object
is created. It has a member function avg that display average of data members.
Code:
#include <iostream>
using namespace std;
class data
{
private :
int numb1, numb2;
public :
data ()
{
numb1 = 100;
numb2 = 200;
}
void add()
{
int sum = numb1 + numb2;
cout << "Sum is " << sum << endl;
}
void avg()
{
int average = (numb1 + numb2) / 2;
cout << "Average is " << average << endl;
}
};
int main ()
{
data d1;
d1.add();
d1.avg();
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
Program:
Write a class TV that contains attributes of Brand Nam, Model and Retail Price. Write a method to
display all attributes and a method to change the attributes. Also write a constructor to initialize all the
attributes.
Code:
#include<iostream>
using namespace std;
class TV{
private:
string BrandName;
string ModelName;
float price;
public:
TV(string Brand,string Model,float p){
BrandName = Brand;
ModelName = Model;
price = p;
}
void change(string Brand,string Model,float p){
BrandName = Brand;
ModelName = Model;
price = p;
}
void display(){
cout<<"Brand: "<<BrandName<<endl;
cout<<"Model: "<<ModelName<<endl;
cout<<"Price: "<<price<<endl;
}
};
int main(){
TV t("Sony", "HDTV", 25430.2);
t.display();
t.change("Toyoshiba", "SHDV",25000);
t.display();
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
LAB 6
Constructor overloading
Program:
Write a class that has num and ch as data members. A constructor with no parameter initialize num to 0
and ch to ‘x’. A constructor with two parameters initializes data members with the given values and
member function show displays the values of data members.
Code:
#include <iostream>
using namespace std;
class chunk{
private:
int num;
char ch;
public:
chunk(){
num = 0;
ch = 'x';
}
chunk(int n, char c){
num = n;
ch = c;
}
void display (){
cout << "Number = " << num << endl;
cout << "Character : " << ch << endl;
}
};
int main (){
chunk c1;
c1.display();
cout << "Please Enter any Number: ";
int numb;
cin >> numb;
cout << "Enter Character: ";
char ch;
cin >> ch;
chunk c2(numb, ch);
c2.display();
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
getline(cin,title);
}
void show(){
cout << "Title: " << title << endl;
cout << "Pages : " << pg << endl;
cout << "Price : " << pr << endl;
}
};
int main (){
Book b1;
b1.get();
Book b2(b1);
Book b3 = b1;
cout<<"\nDetails of Book 1\n";
b1.show();
cout<<"\nDetails of Book 2\n";
b2.show();
cout<<"\nDetails of Book 3\n";
b3.show();
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
}
void add (travel t1){
travel temp;
temp.km = km + t1.km;
temp.hrs = hrs + t1.hrs;
cout << "Now you have traveled " << temp.km << "
kilometers in " << temp.hrs << " hours" << endl;
}
};
int main (){
travel first, second;
first.get();
first.show();
second.get();
second.show();
first.add(second);
}
Output:
Output:
Program:
Write a class Number that has attribute of an array of size 5. A member function input() inputs the values
in array and function min() will calculate minimum number from array and function max() will return
maximum number from array. It has a member function add() that takes an object of type Number, adds
the minimum and maximum number returned by concerned functions of calling objects and parameters
and return an object with sum.
Code:
#include<iostream>
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Lab 7
Static Data Member
Program:
Write a program that counts number of objects created of a particular class.
Code:
#include<iostream>
using namespace std;
class yahoo
{
private:
static int n;
public:
yahoo(){
n++;
}
void show(){
cout<<"You have created "<<n<<" objects so
far."<<endl;
}
};
int yahoo::n = 0;
int main(){
yahoo x,y;
x.show();
yahoo z;
x.show();
Output:
Program:
Write a program that creates three objects of class Student. Each object of class must be assignment a
unique roll number. (Hint: Use static data member for unique roll number.)
Code:
#include<iostream>
using namespace std;
class Student{
private:
static int r;
int rno , marks;
string name;
public:
Student(){
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
r++;
rno=r;
}
void in(){
cout<<"Enter marks: ";
cin>>marks;
cin.ignore();
cout<<"Enter name: ";
getline(cin,name);
}
void show(){
cout<<"Roll.No :"<<rno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Marks : "<<marks<<endl;
}
};
int Student::r = 0;
int main(){
Student s1,s2,s3;
s1.in();
s2.in();
s3.in();
s1.show();
s2.show();
s3.show();
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Static function
Program:
Write a program that creates a class and a static member function show(). Call this function in main
function without creating its object.
Code:
#include<iostream>
using namespace std;
class Test{
private:
static int n;
public:
static void show(){
cout<<"n = "<<n<<endl;
}
};
int Test::n = 10;
int main(){
Test::show();
}
Output:
Program:
Write a program that counts number of objects created of a particular class. The program must be able to
display the result even if no object is being create so far.
Code:
#include<iostream>
using namespace std;
class yahoo{
private:
static int n;
public:
yahoo(){
n++;
}
static void show(){
cout<<"You have created "<<n<<" objects so
far."<<endl;
}
};
int yahoo::n = 0;
int main(){
yahoo::show();
yahoo x,y;
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
x.show();
yahoo z;
x.show();
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Lab 8
Friend Functions
Program:
Write a program that creates two Class A and B. Class A has a data member s and class B has data
member t which is initialized by constructor. Create a friend function which help to get value of data
member of class A in Class B and then show the sum of both data members in class B.
Code:
#include<iostream>
using namespace std;
class B;
class A{
private:
int a;
public:
A(){
a=10;
}
friend void show(A,B);
};
class B{
private:
int b;
public:
B(){
b=20;
}
friend void show(A,B);
};
void show (A x,B y)
{
int r;
r=x.a+y.b;
cout<<"The value of class A object = "<<x.a<<endl;
cout<<"The value of class B object = "<<y.b<<endl;
cout<<"The sum of both value is = "<<r<<endl;
}
int main(){
A obj1;
B obj2;
show(obj1, obj2);
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Friend Class
Program:
Write a program that creates two Class A and B. Declare class A as friend of class B. Two data members
are declared and initialized in class A and are displayed through class B.
Code:
#include<iostream>
using namespace std;
class A{
private:
int a,b;
public:
A(){
a=10;
b=20;
}
friend class B;
};
class B{
public:
void showA(A obj){
cout<<"The value of class a = "<<obj.a<<endl;
}
void showB(A obj){
cout<<"The value of class b = "<<obj.b<<endl;
}
};
int main(){
A x;
B y;
y.showA(x);
y.showB(x);
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Lab 9
Overloading ++ operator
Program:
Write a program that overloads increment operator to work with user defined objects.
Code:
#include<iostream>
using namespace std;
class Count{
private:
int n;
public:
Count(){
n=0;
}
void show(){
cout<<"n : "<<n<<endl;
}
void operator ++(){
n=n + 1;
}
};
int main()
{
Count obj;
obj.show();
++obj;
obj.show();
}
Output:
Code:
#include<iostream>
using namespace std;
class Count{
private:
int n;
public:
Count(){
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
n=0;
}
void show(){
cout<<"n : "<<n<<endl;
}
Count operator ++(){
Count temp;
n=n + 1;
temp.n = n;
return temp;
}
};
int main()
{
Count x,y;
x.show();
y.show();
y = ++x;
x.show();
y.show();
}
Output:
Code:
#include<iostream>
using namespace std;
class Count{
private:
int n;
public:
Count(){
n=0;
}
void show(){
cout<<"n : "<<n<<endl;
}
Count operator ++(){
Count temp;
n=n + 1;
temp.n = n;
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
return temp;
}
Count operator ++(int){
Count temp;
n=n + 1;
temp.n = n;
return temp;
}
};
int main(){
Count x,y;
x.show();
++x;
x++;
x.show();
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Lab 10
Overloading binary operators
Program:
Write a program that overloads binary addition operator +.
Code:
#include<iostream>
using namespace std;
class Add{
private:
int a,b;
public:
Add(){
a = b = 0;
}
void in(){
cout<<" Enter a: ";
cin>>a;
cout<<" Enter b: ";
cin>>b;
}
void show(){
cout<<"a= "<<a<<endl;
cout<<"b= "<<b<<endl;
}
Add operator +(Add p){
Add temp;
temp.a = p.a + a;
temp.b = p.b + b;
return temp;
}
};
int main(){
Add x,y,z;
x.in();
y.in();
z=x + y;
x.show();
y.show();
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
Program:
Write a program that overloads arithmetic addition operator + for concatenation two string values.
Code:
#include<iostream>
#include<string.h>
using namespace std;
class String{
private:
char str[50];
public:
String(){
str[0]='\0';
}
void in()
{
cout<<"Enter string : ";
gets(str);
}
void show(){
cout<<str<<endl;
}
String operator+ (String s){
String temp;
strcpy(temp.str,str);
strcat(temp.str,s.str);
return temp;
}
};
int main(){
String s1,s2,s3;
s1.in();
s2.in();
cout<<"s1 = ";
s1.show();
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
cout<<"s2 = ";
s2.show();
cout<<"s3 = ";
s3.show();
cout<<"concatenating s1 and s2 in s3..."<<endl;
s3= s1+s2;
cout<<"s3 = ";
s3.show();
}
Output:
Program
Write a program that overloads the comparison operator == to work with String class. The result of
comparison must be 1 if two strings are of same length and 0 otherwise.
Code:
#include<iostream>
#include<string.h>
using namespace std;
class String{
private:
char str[50];
public:
String(){
str[0]='\0';
}
void in(){
cout<<"Enter string : ";
gets(str);
}
void show(){
cout<<str<<endl;
}
int operator == (String s){
if(string(s.str)== string(str))
return 1;
else
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
return 0;
}
};
int main(){
String s1,s2;
s1.in();
s2.in();
cout<<"String 1: ";
s1.show();
cout<<"String 2: ";
s2.show();
if(s1==s2){
cout<<"Both strings are equal length"<<endl;
}
else{
cout<<"Both strings are Different length"<<endl;
}
}
Output:
void show(){
cout<<"You have read "<<pages<<"pages in "<<days<<"
days"<<endl;
}
void operator += (Read r){
days=days + r.days;
pages=pages + r.pages;
}
};
int main()
{
Read r1,r2;
r1.in();
r2.in();
cout<<"Reading number 1... "<<endl;
r1.show();
cout<<"Reading number 2... "<<endl;
r2.show();
cout<<"\nAdding r1 to r2 using += operator ..."<<endl;
r2 += r1;
cout<<"\nTotal reading is as follows: "<<endl;
r2.show();
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Lab 11
Inheritance
Program 1:
Create a class Move which contains a data member position, the member function forward() which
increments the value of position by 1. The member function show() will display its value. Suppose a new
class is required. The new class should contain a member function to decrement of the value of position.
In this situation, the class move can be inherited in the new class. The new class will inherit the data
member and member functions of move. It will then add another member function backward() to
decrement the value of position variable.
Code:
#include<iostream>
using namespace std;
class Move{
protected:
int position;
public:
Move(){
position=0;
}
void forward(){
position++;
}
void show(){
cout<<"Position = "<<position<<endl;
}
};
class Move2:public Move{
public:
void backward(){
position--;
}
};
int main(){
Move2 m;
m.show();
m.forward();
m.show();
m.backward();
m.show();
return 0;
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Program 2:
Write a class Person that has the attributes of id, name and address. It has a constructor to initialize, a
member function to input and a member function to display data members. Create another class Students
that inherits Person class. It has additional attributes of roll number and marks. It has member function to
input and display its data members.
Code:
#include<iostream>
using namespace std;
class Person{
protected:
int id;
string name, address;
public:
Person(){
id=0;
name[0]='\0';
address[0]='\0';
}
void getInfo(){
cout<<"Enter your id: "; cin>>id;
cin.ignore();
cout<<"Enter your name: ";
getline(cin, name);
cout<<"Enter your address: "; getline(cin, address);
}
void showInfo(){
cout<<"Your Personal Information is: \n";
cout<<"id = "<<id<<endl;
cout<<"Name = "<<name<<endl;
cout<<"Address = "<<address<<endl;
}
};
class Student:public Person{
private:
int rollNo, marks;
public:
void getEdu(){
cout<<"Enter your roll no: "; cin>>rollNo;
cout<<"Enter your marks: "; cin>>marks;
}
void showEdu(){
cout<<"Your Educational Information is: \n";
cout<<"Roll No = "<<rollNo<<endl;
cout<<"Marks = "<<marks<<endl;
}
};
int main(){
Student s;
s.getInfo();
s.getEdu();
s.showInfo();
s.showEdu();
return 0;
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
Function overriding
Program 3:
Write a program that declares two classes. The parent class is called simple that has two data members a
and b to store two numbers. It also has four member functions:
The add() functions add two numbers and display the result.
The sub() functions subtract two numbers and display the result.
The mul() functions multiply two numbers and display the result.
The div() functions divide two numbers and display the result.
The child class is called complex that override all four functions. Each function in the child class checks
the values of data members. It calls the corresponding member function in the parent class if the values
are greater than 0. Otherwise it displays an error msg.
Code:
#include<iostream>
using namespace std;
class Simple{
protected:
int a,b;
public:
Simple(){
a=b=0;
}
void in(){
cout<<"Enter your a: "; cin>>a;
cout<<"Enter your b: "; cin>>b;
}
void add(){
cout<<"\na + b = "<<a+b<<endl;
}
void sub(){
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
cout<<"\na - b = "<<a-b<<endl;
}
void mul(){
cout<<"\na * b = "<<a*b<<endl;
}
void div(){
cout<<"\na / b = "<<a/b<<endl;
}
};
class Complex:public Simple{
public:
void add(){
if(a<=0 || b<=0){
cout<<"\nInvalid Values\n";
}else{
Simple::add();
}
}
void sub(){
if(a<=0 || b<=0){
cout<<"\nInvalid Values\n";
}else{
Simple::sub();
}
}
void mul(){
if(a<=0 || b<=0){
cout<<"\nInvalid Values\n";
}else{
Simple::mul();
}
}
void div(){
if(a<=0 || b<=0){
cout<<"\nInvalid Values\n";
}else{
Simple::div();
}
}
};
int main(){
Complex obj;
obj.add();
obj.in();
obj.add();
obj.sub();
obj.mul();
obj.div();
return 0;
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
Program 4:
Write a base class Computer that contains data members of wordSize(in bits), memorySize(in
megabytes), storageSize(in megabytes) and speed(in megahertz). Derive a Laptop class that is a kind of
Computer but also specifies the object’s length, width, height and weight. Member functions for class
should include default constructor, a constructor to initialize all components and a function to display data
members.
Code:
Output:
Public Inheritance
Program 5:
Write a program that declares two classes and defines a relationship between them using public
inheritance.
Code:
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Lab 12
Protected inheritance
Program:
Write a program that declares two classes and defines a relationship between them using protected
inheritance.
Code:
#include<iostream>
using namespace std;
class Parent{
public:
int a;
protected:
int b;
private:
int c;
};
class Child: protected Parent{
public:
void in(){
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
void out(){
cout<<"a : "<<a<<endl;
cout<<"b : "<<b<<endl;
}
};
int main(){
Child c;
c.in();
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
c.out();
return 0;
}
Output:
Private inheritance
Program:
Write a program that declares two classes and defines a relationship between them using public
inheritance.
Code:
#include<iostream>
using namespace std;
class Parent{
public:
int a;
protected:
int b;
private:
int c;
};
class Child: private Parent{
public:
void in(){
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
}
void out(){
cout<<"a : "<<a<<endl;
cout<<"b : "<<b<<endl;
}
};
int main(){
Child c;
c.in();
c.out();
return 0;
}
Output:
Multilevel inheritance
Program:
Write a program that declares three classes and defines a relationship among them using multilevel
inheritance.
Code:
#include<iostream>
using namespace std;
class A{
private:
int a;
public:
void in(){
cout<<"Enter a: ";
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
cin>>a;
}
void out(){
cout<<"a : "<<a<<endl;
}
};
class B: public A{
private:
int b;
public:
void in(){
A::in();
cout<<"Enter b: ";
cin>>b;
}
void out(){
A::out();
cout<<"b : "<<b<<endl;
}
};
class C: public B{
private:
int c;
public:
void in(){
B::in();
cout<<"Enter c: ";
cin>>c;
}
void out(){
B::out();
cout<<"c : "<<c<<endl;
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
}
};
int main(){
C obj;
obj.in();
obj.out();
return 0;
}
Output:
Program:
Write a class Person that has the attributes of id, name and address. It has a constructor to initialize, a
member function to input and a member function to display data members. Create another class Students
that inherits Person class. It has additional attributes of roll number and marks. It has member function to
input and display its data members. Create 3rd class scholarship that inherits Student class. It has
additional attributes of scholarship name and amount. It also has member functions to input and display
its data members.
Code:
#include<iostream>
using namespace std;
class Person{
protected:
int id;
string name, address;
public:
Person(){
id=0;
name[0]='\0';
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
address[0]='\0';
}
void input(){
cout<<"Enter your id: "; cin>>id;
cin.ignore();
cout<<"Enter your name: ";
getline(cin, name);
cout<<"Enter your address: "; getline(cin, address);
}
void output(){
cout<<"Your Personal Information is: \n";
cout<<"id = "<<id<<endl;
cout<<"Name = "<<name<<endl;
cout<<"Address = "<<address<<endl;
}
};
class Student:public Person{
private:
int rollNo, marks;
public:
Student(){
//Person::Person();
rollNo = marks = 0;
}
void input(){
Person::input();
cout<<"Enter your roll no: "; cin>>rollNo;
cout<<"Enter your marks: "; cin>>marks;
}
void output(){
Person::output();
cout<<"Your Educational Information is: \n";
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
cout<<"Roll No = "<<rollNo<<endl;
cout<<"Marks = "<<marks<<endl;
}
};
class Scholarship:public Student{
private:
string sname;
long amount;
public:
Scholarship(){
}
void input(){
Student::input();
cout<<"Enter scholarship amount: "; cin>>amount;
cin.ignore();
cout<<"Enter scholarship name: "; getline(cin,sname);
}
void output(){
Student::output();
cout<<"Your Scholarship Information is: \n";
cout<<"Scholarship Name = "<<sname<<endl;
cout<<"Scholarship Amount = "<<amount<<endl;
}
};
int main(){
Scholarship s;
s.input();
s.output();
return 0;
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
Containership
Program:
Write a class result that has an array of three integers as attribute. It has a number function to input and a
member function to display average of array elements. Create another class student that inherits Result
class. It has additional attributes of roll number, name and an object of type Result. It also has member
functions to input and display its data members.
Code:
#include<iostream>
using namespace std;
class Result{
private:
int marks[3];
public:
void input(){
for(int i=0;i<3;i++){
cout<<"Enter Marks: ";
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
cin>>marks[i];
}
}
void output(){
int t=0;
cout<<"\nResult Card\n";
for(int i=0;i<3;i++){
cout<<"Marks: "<<marks[i]<<endl;
t += marks[i];
}
cout<<"Total Marks: "<<t<<endl;
cout<<"Average Marks: "<<float(t)/3.0<<endl;
}
};
class Student{
private:
int rno;
string name;
Result res;
public:
void input(){
cout<<"Enter your roll no: "; cin>>rno;
cin.ignore();
cout<<"Enter your name: "; getline(cin,name);
res.input();
}
void output(){
cout<<"Your Personal Information is: \n";
cout<<"Roll No = "<<rno<<endl;
cout<<"Name = "<<name<<endl;
res.output();
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
}
};
int main(){
Student s;
s.input();
s.output();
return 0;
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Pointer to objects
Program:
Write a class with an integer data member, a function to input and a function to display it. Create an
object of the class using pointer and access its member functions.
Code:
#include<iostream>
using namespace std;
class Test{
private:
int n;
public:
void get(){
cout<<"Enter a number: ";
cin>>n;
}
void show(){
cout<<"Number is : "<<n<<endl;
}
};
int main(){
Test *ptr;
ptr = new Test;
ptr->get();
ptr->show();
return 0;
}
Output:
class B:public A {
public:
virtual void show(){
cout<<"Child class B......."<<endl;
}
};
class C:public A{
public:
void show(){
cout<<"Child class C......."<<endl;
}
};
int main(){
A a;
B b;
C c;
A *ptr;
ptr = &a;
ptr->show();
ptr = &b;
ptr->show();
ptr = &c;
ptr->show();
return 0;
}
Output:
Virtual Function
Program:
Write a program to implement virtual function in polymorphism
Code:
#include<iostream>
using namespace std;
class A{
public:
virtual void show(){
cout<<"Parent class A......."<<endl;
}
};
class B:public A {
public:
virtual void show(){
cout<<"Child class B......."<<endl;
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
};
class C:public A{
public:
void show(){
cout<<"Child class C......."<<endl;
}
};
int main(){
A a;
B b;
C c;
A *ptr;
ptr = &a;
ptr->show();
ptr = &b;
ptr->show();
ptr = &c;
ptr->show();
return 0;
}
Output:
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Lab 14
Function templates
Program 1:
Write a template that accepts two numeric values and displays the maximum numbers.
Code:
#include<iostream>
using namespace std;
template <class Type>
Type Max(Type a, Type b){
if(a>b){
return a;
}else{
return b;
}
}
int main(){
int n;
float m;
n = Max(23,34);
m = Max(2.3, 5.2);
cout<<"Maximum of two integers is: "<<n<<endl;
cout<<"Maximum of two floats is: "<<m<<endl;
return 0;
}
Output:
Program 2:
Write a function template that accepts value of any type and then display it.
Code:
#include<iostream>
using namespace std;
template <class Type>
void show(Type a){
cout<<a<<endl;
}
int main(){
show(100);
show("Hello");
show("Hussain Sajid");
return 0;
}
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Output:
Program 3:
Write a function template to find minimum value in an array and return it.
Code:
#include<iostream>
using namespace std;
template <class T>
T findMin(T arr[], int n){
int i;
T min;
min=arr[0];
for(i=0;i<n;i++){
if(min>arr[i]){
min=arr[i];
}
}
return min;
}
int main(){
int iarr[] = {1,2,3,4,5};
char carr[] = {'h','u','s','s','a','i','n'};
double darr[] = {1.1,2.2,3.3,4.4,5.5,6.6};
cout<<"Generic Function to find minimum in array"<<endl;
cout<<"Integer Minimum is "<<findMin(iarr,5)<<endl;
cout<<"Character Minimum is "<<findMin(carr,7)<<endl;
cout<<"Double Minimum is "<<findMin(darr,6)<<endl;
return 0;
}
Output:
Program 4
Write a function template that accepts three parameters and display them in reverse order.
<Student Practice>
DEPARTMENT OF CS OBJECT ORIENTED PROGRAMMING
Class Template
Program 5:
Write a class template that stores five values of any type in an array.
Code:
#include<iostream>
using namespace std;
template <class T>
class Test{
private:
T arr[5];
public:
void input(){
for(int i=0;i<5;i++){
cin>>arr[i];
}
}
void output(){
cout<<"The values in array are as follows"<<endl;
for(int i=0;i<5;i++){
cout<<arr[i]<<"\t";
}
cout<<endl;
}
};
int main(){
Test <int>x;
Test <char>y;
Output:
Program 6:
Write a class template that inputs the index of the array and display the value in the specific index.
<Student Practice>