0% found this document useful (0 votes)
67 views18 pages

OOP Using C++ Practical Mannual

The document contains details about a practical list for the Object Oriented Programming using C++ course for the Computer Science and Engineering program at G H Raisoni University. It includes 10 programming problems to implement concepts like classes, objects, arrays, constructors, operator overloading, inheritance and templates. For each problem, it provides the title, problem definition, sample code, input/output specifications, and conclusions about the concepts demonstrated.

Uploaded by

Vijay Gadicha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
67 views18 pages

OOP Using C++ Practical Mannual

The document contains details about a practical list for the Object Oriented Programming using C++ course for the Computer Science and Engineering program at G H Raisoni University. It includes 10 programming problems to implement concepts like classes, objects, arrays, constructors, operator overloading, inheritance and templates. For each problem, it provides the title, problem definition, sample code, input/output specifications, and conclusions about the concepts demonstrated.

Uploaded by

Vijay Gadicha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

G H RAISONI UNIVERSITY, AMRAVATI

Anjangaon Bari Road, Amravati - 444701

SCHOOL OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

NAME OF PROGRAM: B TECH CSE

PRACTICAL LIST

Name of Course: OOP Programming using C++ Course Code: DC106P408


Year/Semester: II/IV Session: 2020-21

Course Outcome: after completion of the course, students will be able to

CO1. Understanding the features of C++ supported object oriented programming & implementation of
operator overloading.

CO2. Problem solving using constructors & virtual functions.

CO3. Analysis of stream class hierarchy & standard template library.

Sr. No Name of Practical CO

1 WAP in C++ to create a class called as student. Data members are roll_no, name & 01
fees of the student. Write appropriate get () & put () functions to scan and display
the student data.
2 WAP in C++ to create a class called as employee. Data members are eid, sal & 01
name of the employee. Scan the data for 10 such employees & display the same by
using array of objects.
3 WAP in C++ to create a class called as Book. Data members are name of the Book 01
& price. Write default, parameterized & copy constructors to initialize & display
Book object values.
4 WAP in C++ to create a class called as Distance, members are ft & in. Assign 01
appropriate values to objects D1 & D2 and add their values by overloading binary
‘+’ operator to store the result in D3.
5 WAP in C++ to create a class called as student, having member as roll_no & name 02
of the student. Create another class Exam having members as mark1 & mark2.
Finally create a class called result which is derived from student & Exam.
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Write a show function in it to show student info & percentage of marks scored
using Multiple Inheritance.
6 WAP in C++ to demonstrate Derived class constructor. 02
7 WAP in C++ to implement friend function acting as a bridge between the two 02
classes.
8 WAP in C++ to implement Virtual function for function overriding. 02
9 WAP in C++ to overload insertion (>>) & extraction (<<) operators for objects. 03
10 WAP to implement function Template. 03

Subject Teacher
(Dr Vijay B Gadicha)
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 01

Title/Aim Implementing class and objects in C++.

Problem Definition WAP in C++ to create a class called as student. Data members are rollno, name &
fees of the student. Write appropriate get () & put () functions to scan and display
the student data.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code #include<iostream>


using namespace std;
class stud
{
int rollno;
float fees;
char name[20];
public:
void gets()
{
cin>>rollno>>fees>>name;
}
void puts()
{
cout<<rollno<<fees<<name;
}
};
main()
{
stud s;
s.gets();
s.puts();
}
Input & output Input :
specifications
101
8000
Bond
Output: 101 8000 Bond

Result This program demonstrates the creation of classes & objects in C++.

Conclusion Here, we have implemented classes & objects in C++.


G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 02

Title/Aim Implementing array of objects in C++

Problem WAP in C++ to create a class called as employee. Data members are eid, sal & name of
Definition the employee. Scan the data for 10 such employees & display the same by using array
of objects.

Software/Har Online C++ compiler, online GDB.


dware
Requirement

Program code #include<iostream>


using namespace std;
class emp
{
int eid;
float sal;
char nam[20];
public:
void gete()
{
cin>>id>>sal>>nam;
}
void pute()
{
cout<<id<<sal<<nam;
}
};
int main()
{
emp e[10];
inti;
for (i=0; i<10; i++)
{
e[i].gete();
}
for (i=0; i<10; i++)
{
e[i].pute();
}
};

Input :

1 8000 Edward
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Input & 2 7000 Bella


output 3 6000 James
specifications 4 7000 Jacob
5 8000 Tom
6 7000 Esme
7 8000 Alice
8 5000 Rose
9 6000 Emmett
10 8000 Jerry
Output:
1800Edward27000Bella36000James47000Jacob58000Tom67000Esme78000Alice8500
0Rose96000Emmett108000Jerry

Result This program demonstrates the use of arrays in C++.

Conclusion Here, we have implemented array of objects in C++.


G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 03

Title/Aim Initialization of object using default, parameterized and copy constructor.

Problem Definition WAP in C++ to create a class called as Book. Data members are name of the
Book & price. Write default, parameterized & copy constructors to initialize &
display Book object values.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code #include <iostream>


#include <string.h>
Using namespace std;
class book
{
char name[20];
int pr;
public:
book()
{
strcpy (name,”Intro to C++”);
pr=1000;
}
book(char n[], int a)
{
*name=*n;
pr=a;
}
book(book &b)
{
strcpy (name,b.name);
pr=b.pr;
}
void show()
{
cout<<name<<endl;
cout<<pr<<endl;
}
};
main()
{
book b1;
book b2;
book b3(b2);
b1.show();
b2.show();
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

b3.show();
}

Input & output Input :


specifications
Intro to C++
1000
Intro to C++
1000
Intro to C++
1000

Output:
Intro to C++
1000
Intro to C++
1000
Intro to C++
1000

Result This program demonstrates the constructors in C++.

Conclusion Here we have implemented the constructor in C++.


G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 04

Title/Aim Implementing Binary Operator Overloading.

Problem Definition WAP in C++ to create a class called as Distance, members are ft & in. Assign
appropriate values to objects D1 & D2 and add their values by overloading binary
‘+’ operator to store the result in D3.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code #include <iostream>


Using namespace std;
class dist
{
int ft, in;
public:
void getd()
{
cout<<” Enter the Feet and Inches:”;
cin>>ft>>in;
}
dist operator + (dist & d)
{
dist temp ;
temp.ft=ft+d.ft;
temp.in=in+d.in;
if(temp.in>=12)
{
temp.in=temp.in-12;
temp.ft=temp.ft+1;
}
return temp;
}
void show ()
{
cout<<”Feets=”<<ft<<”Inches=”<<in;
}
};

main()
{
dist d1, d2,d3;
d1.getd();
d2.getd();
d3=d1+d2;
d3.show();
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Input & output Input :-


specifications Enter the Feet and Inches:10 11
Enter the Feet and Inches:5 9
Output :-
Feets=16Inches=8
Result This program demonstrates Binary operator overloading in C++.

Conclusion Here, we have implemented Binary operator overloading in C++.


G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 05

Title/Aim Implementing multiple inheritance in C++.

Problem Definition WAP in C++ to create a class called as student, having member as roll_no &
name of the student. Create another class Exam having members as mark1 &
mark2. Finally create a class called result which is derived from student & Exam.
Write a show function in it to show student info & percentage of marks scored
using Multiple Inheritance.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code # include <iostream>


using namespace std;
class stud
{
public:
int rollno;
char name[20];
};
class exam
{
public:
int Mark1;
int Mark2;
};
class result : public stud, public exam
{
float Per;
public:
void get()
{
cin>>rollno>>name>>Mark1>>Mark2;
}
void show ()
{
cout<<rollno<<name<<"Per"<<((Mark1+Mark2)*100)/200;
}
};

main()
{
result r;
r.get();
r.show();
}
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Input & output Input :-


specifications 12 Garry 80 88

Output :-
12GarryPer84
Result This example demonstrates multiple inheritance in C++.

Conclusion Here, we have implemented multiple inheritance in C++.


G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 06

Title/Aim Implementing derived class constructor in C++.

Problem Definition WAP in C++ to demonstrate Derived class constructor.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code # include <iostream>


using namespace std;
class base
{
public:
base( )
{
cout<<" This is a base class";
}
};
class derived : public base
{
public :
derived ( )
{
cout<<"\n This is derived class";
}
};
main( )
{
derived d;
}

Input & output Output :-This is a base class.


specifications
This is derived class.

Result Here, we have implemented the class called as base, inherited this base class into
derived class to demonstrate the derived class constructor mechanism.

Conclusion Here, we have implemented derived class constructor in C++.


G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 07

Title/Aim Implementing friend function acting as a bridge between the two classes in C++.

Problem Definition WAP in C++ to implement friend function acting as a bridge between the two
classes.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code # include <iostream>


using namespace std;
class no2;
class no1
{
int x;
public:
void getx()
{
cout<<"Enter first no:";
cin>>x;
}
friend int add (no1& n1, no2& n2);
};
class no2
{
int y;
public:
void gety()
{
cout<<"Enter second no:";
cin>>y;
}
friend int add (no1& n1, no2& n2);
};
int add (no1& n1, no2& n2)
{
cout<< n1.x+n2.y;
}
main()
{
no1 N1;
no2 N2;
N1.getx();
N2.gety();
add(N1,N2);
}
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Input & output Input:


specifications
Enter first no:99
Enter second no:111
Output: 210

Result This C++ program demonstrates the functionality of friend function acting as a
bridge between the two classes.

Conclusion Here, we have implemented friend function in C++.


G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 08

Title/Aim Implementing virtual function for function overriding in C++.

Problem Definition WAP in C++ to implement Virtual function for function overriding.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code # include <iostream>


using namespace std;
class shape
{
public:
virtual void show()
{
cout<<"This is base class called 'Shape'"<<endl;
}
};
class circle: public shape
{
public:
void show()
{
cout<<"This is circle"<<endl;
}
};
class triangle: public shape
{
public:
void show()
{
cout<<"This is triangle"<<endl;
}
};
main()
{
shape s;
circle c;
triangle t;
shape *sptr;
sptr=&s;
sptr->show();
sptr=&c;
sptr->show();
sptr=&t;
sptr->show();
}
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Input & output Output: This is Base class called ‘Shape’.


specifications
This is circle.

This is triangle.

Result Here, we have implemented a class called as Shape, Circle & Triangle using
virtual function for function overriding in C++.

Conclusion Here, we have implemented virtual function for function overriding in C++.
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 09

Title/Aim Overloading insertion and extraction operators for objects.

Problem Definition WAP in C++ to overload insertion (>>) & extraction (<<) operators for objects.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code #include<iostream>


using namespace std;
class number
{
char a;
int b;
float c;
public:
friend istream& operator >> (istream&in, number &n)
{
in>>n.a>>n.b>>n.c;
}
friend ostream& operator<< (ostream&out, number &n)
}
out<<n.a<<n.b<<n.c;
}
};
main()
{
number N;
cin>>N;
cout<<N;
}

Input & output input:


specifications
x
11
3.14
output:

x 11 3.14

Result Here, we have overloaded insertion (>>) and extraction (<<) operators for objects
in C++.

Conclusion Here, we have implemented insertion and extraction operators for objects in C++.
G H RAISONI UNIVERSITY, AMRAVATI
Anjangaon Bari Road, Amravati - 444701

Practical No 10

Title/Aim Implementation of function template.

Problem Definition WAP to implement function Template.

Software/Hardware Online C++ compiler, online GDB.


Requirement

Program code #include <iostream>


using namespace std;
template <class T>
T add ( T x, Ty)
{
return(x+y);
}
main()
{
cout<<add<int>(999,1)<<endl;
cout>>add<float>(4.4,5.5)<<endl;
cout<<add<double>(12.33,13.55);
}
Input & output Output:
specifications
1000
9.9
25.88
Result This program demonstrates the function templates for int, float & double built-in
data types.

Conclusion Here, we have implemented function template in C++.

You might also like