0% found this document useful (0 votes)
14 views

oopt-c-lab-r20-1

The document is a lab manual for Object-Oriented Programming through C++ for B.Tech II Year I Semester students at Jawaharlal Nehru Technological University, Kakinada. It contains a series of experiments focusing on various C++ programming concepts such as classes, constructors, destructors, operator overloading, inheritance, and templates. Each experiment includes a brief description, code examples, and expected outputs.

Uploaded by

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

oopt-c-lab-r20-1

The document is a lab manual for Object-Oriented Programming through C++ for B.Tech II Year I Semester students at Jawaharlal Nehru Technological University, Kakinada. It contains a series of experiments focusing on various C++ programming concepts such as classes, constructors, destructors, operator overloading, inheritance, and templates. Each experiment includes a brief description, code examples, and expected outputs.

Uploaded by

hanusmurukutla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

OOPT C++ LAB R20-1

objected oriented programming through c++ (Jawaharlal Nehru Technological


University, Kakinada)

Scan to open on Studocu

Downloaded by Hanumantharao Murukutla


Studocu is not sponsored or endorsed by any college or university

Downloaded by Hanumantharao Murukutla


OBJECT ORIENTED PROGRAMMING THROUGH C++ LAB MANUAL

B.Tech II Year I Semester (JNTUK) (R20)

Downloaded by Hanumantharao Murukutla


INDEX

EXP.NO. NAME OF THE EXPERIMENT PAGE


NO
1 Write a main function to create objects of DISTANCE class. Input 3
two distances and output the sum.
2 Write a C++ Program to illustrate the use of Constructors and 5
Destructors (use the above program.)
3 Write a program for illustrating function overloading in adding 7
the distance between objects (use the above problem)
4 Write a program implementing Friend Function 9
5 Write a program to illustrate this pointer 10
6 Write a Program to illustrate pointer to a class 11
7 Write a program to Overload Unary, and Binary Operators as 13
Member Function, and Non Member Function.
I. Unary operator as member function
II. Binary operator as non member function
8 Write a c ++ program to implement the overloading 17
assignment = operator
9 C++ program to demonstrate example of single inheritance. 19

10 C++ program to demonstrate example of hierarchical 21


inheritance to get square and cube of a number.

11 Also illustrate the order of execution of constructors and 23


destructors in inheritance
12 Write a C++ Program to illustrate template class 25
13 Write a Program to illustrate member function templates 26
14 Write a C++ program illustrating user defined string processing 27
functions using pointers (string length, string copy, string
concatenation)
15 Write C++ program that implement Bubble sort, to sort a 30
given list of
integers in ascending order

Downloaded by Hanumantharao Murukutla


1. Write a main function to create objects of DISTANCE class. Input two
distances and output the sum.

#include<iostream

> using namespace

std; class dist

public:

int feet,inch,x,y,z;

void input()

cout<<"enter feet and inches:"<<"\

n"; cin>>feet>>inch;

void show()

cout<<"The distance is ";

cout<<feet<<" feet

"<<inch<<"inch\n";

void sum(dist x,dist y)

feet=x.feet+y.fe

et;

inch=x.inch+y.in

ch; if(inch>=12)

Downloaded by Hanumantharao Murukutla


3

Downloaded by Hanumantharao Murukutla


feet=feet+
1;
inch=inch-
12;

};

int main()

dist x,y,z;x.input();

y.input();

z.sum(x,y);

z.show();
}

OUTPUT:

Enter feet and inches:


6 3
Enter feet and inches:
3 11
The distance is: 10 feet and 2 inches

Downloaded by Hanumantharao Murukutla


4

Downloaded by Hanumantharao Murukutla


2. Write a C++ Program to illustrate the use of Constructors and
Destructors (use theabove program.)

#include<iostream>
using namespace
std; class dist
{
public:
int
feet,inch;
dist()
{
}
dist (int x,int y)
{
feet=x;
inch =y;
}
dist (float m,float n)
{
feet=
m;
inch=
n;
}
void display()
{
cout<<"Resulted distance is: ";
cout<<feet<<" feet "<<inch<<"
inch\n";
}
void sum(dist obj1,dist obj2)
{
feet=obj1.feet+obj2.feet;
inch=obj1.inch+obj2.inch;
if(inch>=12)
{

Downloaded by Hanumantharao Murukutla


5

Downloaded by Hanumantharao Murukutla


feet=feet+
1;
inch=inch-
12;
}
}
~dist()
{
cout<<"object deleted:\n";
}
};
int main()
{
dist obj1(8,9),obj2(4.0f,7.3f),obj3;
obj3.sum(obj1,obj2);
obj3.display();
}

OUTPUT:-

object deleted:

object deleted:

Resulted distance is: 13 feet 4 inch

object deleted:

object

deleted:

object

deleted:

Downloaded by Hanumantharao Murukutla


6

Downloaded by Hanumantharao Murukutla


3. Write a program for illustrating function overloading in adding the
distance between objects (use the above problem).

#include
<iostream> using
namespace std;
class Distance
{
private:
int feet;
int
inches;
public:
void set_distance()
{
cout<<"Enter feet:
"; cin>>feet;
cout<<"Enter
inches: ";
cin>>inches;
}
void get_distance()
{
cout<<"Distance is feet= "<<feet<<",
inches="<<inches<<endl;
}
void add(Distance d1, Distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches +
d2.inches; feet = feet +
(inches / 12);
}
void add(Distance *d1, Distance *d2)
{
feet = d1->feet + d2->feet;
inches = d1->inches + d2->inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
};
int
main()
{
Distance
d1,d2,d3;
d1.set_distance(
);
d2.set_distance(
);
d3.add(d1,d2);

Downloaded by Hanumantharao Murukutla


d3.get_distance(
); d3.add(&d1,
&d2);
d3.get_distance(
); return 0 ;
}

Downloaded by Hanumantharao Murukutla


OUTPUT:-

Enter feet: 3
Enter inches: 2
Enter feet: 4
Enter inches: 5
Distance is feet= 7, inches= 7
Distance is feet= 7, inches= 7

Downloaded by Hanumantharao Murukutla


4. Write a program implementing Friend Function

#include
<iostream> using
namespace
std;class A
{
Public :
int x ;
friend class B; // friend class.
};
class B
{
public:
void display(A &a)
{
cout<<"value of x is : "<<a.x;
}
};
int main()
{
A a;
a.x=20
;
B b;

Clrscr();

b.display(a);
return 0;
}

Output:

Value of x is: 20

Downloaded by Hanumantharao Murukutla


9

Downloaded by Hanumantharao Murukutla


5. Write a program to illustrate this pointer

#include <iostream>
/* Local variable is same as a member
name. class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'

this->x = x;
}
void print() { cout << "x = " << x << endl; }
};

int main()
{
Test obj;
int x =
20;
obj.setx(x
);
obj.print();
return 0;
}

OUTPUT:-

X=20

Downloaded by Hanumantharao Murukutla


10

Downloaded by Hanumantharao Murukutla


6. Write a Program to illustrate pointer to a class

#include
<iostream> class A
{
private:
int
x; int
y;
public
:
A(int x, int y)
{
this->x =
x; this-
>y = y;
}
void display()
{
cout<<"x =
"<<x<<endl;
cout<<"y =
"<<y<<endl;
}
};
int main()
{
A *ptr = new A(10, 30);
//Here ptr is pointer
to class A
ptr-
>display();
return 0;
}

Downloaded by Hanumantharao Murukutla


11

Downloaded by Hanumantharao Murukutla


OUTPUT:-

X =10

Y = 30

12

Downloaded by Hanumantharao Murukutla


7. Write a program to Overload Unary, and Binary Operators as
Member Function, and Non Member Function.

a) unary operators

#include<iostream>

class increment

int

a,b;

public

increment(int x,int y)

a=x;

b=y;

increment( )

void show( )

cout<<"a="<<a<<",b="
<< b<<endl;

void operator ++( );

};

void increment::operator
++( )

Downloaded by Hanumantharao Murukutla


{
13

Downloaded by Hanumantharao Murukutla


a=+

+a;

b=+

+b;

main( )

increment u(10,20);

cout<<"Before
increment u:";

u.show( );

++u;

cout<<"After
increment u:";

u.show( );

Downloaded by Hanumantharao Murukutla


14

Downloaded by Hanumantharao Murukutla


B) Binary:-
#include<iostream>

#include<iomanip>

class x

public:

int

a,b; x

(){}

x(int x1,int y1)

a=x1;b=y1;

void show()

cout<<"a="<<a<<",b="<<b<<endl;

friend x operator *(x x1,x x2);

};

x operator *(x x1,x x2)

x x3;

x3.a=x1.a*x2.a;

x3.b=x1.b*x2.b;

return(x3);

main()

Downloaded by Hanumantharao Murukutla


15

Downloaded by Hanumantharao Murukutla


x x1(1,2),x2(3,4),x3;

x3=x1*x2;

cout<<"Object x1:";

x1.show();

cout<<"Object x2:";

x2.show();

cout<<"Object x3:";

x3.show();

16

Downloaded by Hanumantharao Murukutla


8. Write a c ++ program to implement the overloading assignment
= operator

#include
<iostream> class
Distance {
private:
int feet;

int
inches;
public:
// required constructors
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches =
i;
}
void operator = (const Distance &D )
{
feet = D.feet;
inches =
D.inches;
}

void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
}
};

int main()
{
Distance D1(11, 10), D2(5, 11);
Downloaded by Hanumantharao Murukutla
17

Downloaded by Hanumantharao Murukutla


cout << "First Distance : ";
D1.displayDistance();
cout << "Second Distance :";
D2.displayDistance();
D1 = D2;
cout << "First
Distance :";
D1.displayDistance();
return 0;
}

OUTPUT:-

First Distance : F: 11 I:10


Second Distance :F: 5 I:11
First Distance :F: 5 I:11

18
Downloaded by Hanumantharao Murukutla
9. C++ program to demonstrate example of single inheritance.
#include<iostrea

m>

#include<iomanip

>

#include<string.h

> class student

public:

int rno;

char

name[20];

char

group[5];

public:

void getdata()

cout<<"\n Enter student roll no,name and

group:"; cin>>rno>>name>>group;

void putdata()

cout<<"\n Roll

no:"<<rno; cout<<"\n

Name:"<<name;

cout<<"\n

Branch:"<<group;

Downloaded by Hanumantharao Murukutla


};

class test :public student

public:

19

Downloaded by Hanumantharao Murukutla


int s1,s2,s3;

void

getmarks()

cout<<"\n Enter marks in 3 subjects:";

cin>>s1>>s2>>s3;

void putmarks()

cout<<"\n

subject1:"<<s1;

cout<<"\n

subject2:"<<s2;

cout<<"\n

subject3:"<<s3;

void findtotal()

int

total=s1+s1+s3

; float

avg=total/3;

putdata();

putmarks();

cout<<"\n Total

marks:"<<total; cout<<"\n

Average marks:"<<avg;

Downloaded by Hanumantharao Murukutla


};

20

Downloaded by Hanumantharao Murukutla


10. C++ program to demonstrate example of hierarchical inheritance to

get square and cube of a number.

#include <iostream>

using namespace

std; class number

private:

int num;

public:

void getnumber()

cout << "Enter an integer number: ";

cin >> num;

int returnnumber()

return num;

};

class square:public number

21

Downloaded by Hanumantharao Murukutla


public:

int getsquare()

int num,sqr;

num=returnnumber();

sqr=num*num;

return (sqr);

};

class cube:public number

public:

int getcube(void)

int num,cube;

num=returnnumber();

cube=num*num*num;

return (cube);

};

main()

22

Downloaded by Hanumantharao Murukutla


{

square s;

cube c;

s.getnumber();

cout << "Square of "<< s.returnnumber() << " is: " <<s.getsquare() << endl;

c.getnumber();

cout << "Cube of "<< c.returnnumber() << " is: " << c.getcube()<< endl;

11. illustrate the order of execution of constructors and destructors in inheritance.

// C++ program to show the order of constructor call

// in single inheritance

#include <iostream>

using namespace

std;

// base class

class Parent

public:

// base class

constructor Parent()

23
Downloaded by Hanumantharao Murukutla
{

cout << "Inside base class" << endl;

};

// sub class

class Child : public Parent

public:

//sub class constructor

Child()

cout << "Inside sub class" << endl;

};

// main

function int

main() {

// creating object of sub

class Child obj;

return 0;

24
Downloaded by Hanumantharao Murukutla
12. Write a C++ Program to illustrate template class

#include

<iostream> using

namespace std;

template<class T>

class A

public:

T num1 =

5; T num2

= 6; void

add()

std::cout << "Addition of num1 and num2 : " << num1+num2<<std::endl;

};

int main()

A<int>

d;

d.add();

return

0;

OUTPUT:--

Downloaded by Hanumantharao Murukutla


Addition of num1 and num2 : 11

25

Downloaded by Hanumantharao Murukutla


13. Write a Program to illustrate member function templates.

#include<iostream

> using namespace

std; template<class

E>

void exchange (E&a,E&b)

temp=a;

a=b;

b=temp;

};

int main()

int

x=5,y=8;

float a,b;

cout<<“Enter two values:\

n”; cin>>a>>b;

cout<<“Before exchange\n x=”<<x<<“\

ty=”<<y<<endl; exchange(x,y);

cout<<“After exchange\n x=”<<x<<“\ty=”<<y<<endl;

cout<<“Before exchange\n a=”<<a<<“\

tb=”<<b<<endl; exchange(a,b);

cout<<“After exchange\n a=”<<a<<“\

tb=”<<b<<endl; return 0;

Downloaded by Hanumantharao Murukutla


26

Downloaded by Hanumantharao Murukutla


OUTPUT:--

Enter two values:


63.25 52.36
Before
exchange x=
5 y= 8
After
exchange x=
8 y=5
Before
exchange
a= 63.25 b= 52.36
After exchange
a= 52.36 b= 63.25

14. Write a C++ program illustrating user defined string processing functions (string
length, string copy, string concatenation).

String length:

#include <iostream.h>

#define MAX_SIZE 100 // Maximum size of the

string int main() {

char

text[MAX_SIZE];

char * str = text;

int count = 0;

// Input string from user

cout<<"Enter any string: ";

cin>>text;

// Iterating though last element of the

string while(*(str++) != '\0') count++;

cout<<"Length of "<<text<<" is

"<<count; return 0;

}
Downloaded by Hanumantharao Murukutla
27

Downloaded by Hanumantharao Murukutla


OUTPUT:

Enter any string :

VSMCOE Length of

VSMCOE is 6

String copy:

#include<iostream.h>

#include<stdio.h>

int main()

char strOrig[100],

strCopy[100]; char *origPtr,

*copPtr; cout<<"Enter the

string: "; gets(strOrig);

origPtr =

&strOrig[0]; copPtr

= &strCopy[0];

while(*origPtr)

*copPtr =

*origPtr;

origPtr++;

copPtr++;

*copPtr = '\0';

cout<<"\nEntered String:

"<<strOrig; cout<<"\nCopied

Downloaded by Hanumantharao Murukutla


String: "<<strCopy;

cout<<endl;

28

Downloaded by Hanumantharao Murukutla


return 0;

OUTPUT:

Entered String: Hello codes

cracker Copied String: Hello

codes cracker

string concatenation:

#include <iostream>

#define MAX_SIZE 100 // Maximum size of the

string int main() {

char str1[MAX_SIZE],

str2[MAX_SIZE]; char * s1 = str1;

char * s2 = str2;

// Input 2 strings from

user cout<<"Enter 1st

string: "; cin>>str1;

cout<<"Enter 2nd

string: "; cin>>str2;

// Moving till the end of

str1 while(*(++s1));

// Coping str2 to str1

while(*(s1++) = *(s2++));

cout<<"Concatenated

string:"<<str1; return 0;

29
Downloaded by Hanumantharao Murukutla
OUTPUT:

Enter 1st string: VSM

Enter 2nd string: COE

Concatenated string: VSMCOE

15. Write C++ program that implement Bubble sort, to sort a given list of integers
in ascending order.

#include<iostrea

m> int main()

int num, i, arr[50], j, temp;

cout<<"\n Enter Total Number of

Elements : "; cin>>num;

cout<<"\n Enter "<<num<<" Elements

: \n\n"; for(i=0; i<num; i++)

cout<<" ";

cin>>arr[i];

cout<<"\n\n Sorting Array using Bubble

Sort... \n"; for(i=0; i<(num-1); i++)

for(j=0; j<(num-i-1); j++)

if(arr[j]>arr[j+1])

30

Downloaded by Hanumantharao Murukutla


temp=arr[j];

arr[j]=arr[j+1

];

arr[j+1]=tem

p;

cout<<"\n Sorted Element in Ascending Order : \

n"; for(i=0; i<num; i++)

cout<<" ";

cout<<arr[i]<

<" ";

return 0;

OUTPUT:

Enter Total Number of

Elements : 6 Enter 6 Elements :

12

45

98

63

99

Sorting Array using Bubble sort...

Downloaded by Hanumantharao Murukutla


Sorted Element in Ascending order : 2 12 45 63 98 99

31

Downloaded by Hanumantharao Murukutla

You might also like