CS-307 - OOPS THROUGH C++ Lab (1-18)
CS-307 - OOPS THROUGH C++ Lab (1-18)
LAB MANUAL
FOR
LAB
Ponnekal village,Khammam-507170
CS-307-C++ Lab
Pre requisites
Course
Outcome
CO1 Use of I/O operators, loops, Classes, objects and functions
Course Content
Unit Hours/
Unit Name
No Periods
1 I/O operators, loops, Classes, objects and functions 13
2 Constructors, Destructors and Operator overloading 13
3 Derived classes and types of inheritance 13
4 Templates 6
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 2
OBJECT ORIENTED PROGRAMMING THROUGH C++ LAB COMPUTER SCIENCE & ENGINEERING
Total 45
Course outcomes
Course Teaching
CL Linked PO
Outcome Hours
Write programs using classes, objects and
CO1 R, U, A 1,2,3,4,7 15
functions
Construct programs using Constructors,
CO2 R, U, A 1,2,3,4,7 15
Destructors and Operator overloading
Demonstrate Derived classes and types of
CO3 R,U, A 1,2,3,4,7 15
Inheritance
CO4 Develop programs using Templates U, A 1,2,3,4,7 10
Total
45
Sessions
Reference Books:
E-References:
1. https://www.tutorialspoint.com/cplusplus/index.htm
2. http://www.cplusplus.com/doc/tutorial/
3. https://www.programiz.com/cpp-programming
4. https://beginnersbook.com/2017/08/c-plus-plus-tutorial-for-beginners/
5. http://www.cplusplus.com/files/tutorial.pdf
List of Experiments
#include <iostream.h>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}
OUTPUT:
Enter an integer: 70
The number is: 70
int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A';
70
256.783
character: A
2. Write programs using if/ if – else/ nested if statement.
i) C++ if Statement
#include <iostream.h>
using namespace std;
int main() {
int number;
return 0;
}
OUTPUT:
Enter an integer: 5
You entered a positive number: 5
This statement is always executed.
#include <iostream.h>
using namespace std;
int main() {
int number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 8
OBJECT ORIENTED PROGRAMMING THROUGH C++ LAB COMPUTER SCIENCE &
ENGINEERING
}
cout << "This line is always printed.";
return 0;
}
OUTPUT:
Enter an integer: 4
You entered a positive integer: 4.
This line is always printed.
#include <iostream.h>
using namespace std;
int main() {
int num;
// outer if condition
if (num != 0) {
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 9
OBJECT ORIENTED PROGRAMMING THROUGH C++ LAB COMPUTER SCIENCE &
ENGINEERING
// inner if condition
if (num > 0) {
cout << "The number is positive." << endl;
}
// inner else condition
else {
cout << "The number is negative." << endl;
}
}
// outer else condition
else {
cout << "The number is 0 and it is neither positive nor negative." << endl;
}
return 0;
}
OUTPUT:
Enter an integer: 35
The number is positive.
This line is always printed.
#include <iostream.h>
using namespace std;
int main() {
int number;
int sum = 0;
return 0;
}
OUTPUT:
Enter a number: 6
Enter a number: 12
Enter a number: 7
Enter a number: 0
Enter a number: -2
The sum is 25
ii) Sum of Positive Numbers using Do-while loop.
#include <iostream.h>
using namespace std;
int main() {
int number = 0;
int sum = 0;
do {
sum += number;
return 0;
}
OUTPUT:
Enter a number: 6
Enter a number: 12
Enter a number: 7
Enter a number: 0
Enter a number: -2
The sum is 25
iii) Find the sum of first n Natural Numbers using for loop
#include <iostream.h>
int main() {
int num, sum;
sum = 0;
return 0;
}
OUTPUT:
Enter a positive integer: 10
Sum = 55
int main() {
return 0;
}
OUTPUT:
#include <iostream.h>
using namespace std;
int main() {
int numbers[5];
return 0;
}
OUTPUT:
Enter 5 numbers:
11
12
13
14
15
The numbers are: 11 12 13 14 15
#include <iostream.h>;
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
return 0;
}
OUTPUT:
Area of Room = 1309
Volume of Room = 25132.8
class code
{ int id;
int count;
public:
code()
{
cout<<"Default constructor called\n";
id=0;
cout<<"id="<<id<<<"Parameterized constructor called\n";
id=a;
cout<<"id="<<id<<<"copy constructor called\n";
id=x.id;
cout<<"id="<<id<<<"id="<<id<<<"Object Destroyed"<<<"\n For object d id=";
d.display();
cout<<"\n For object a id="; a.display();
cout<<"\n For object b id="; d.display();
cout<<"\n For object c id="; d.display(); return 0;
}
OUTPUT:
#include <iostream.h>
using namespace std;
// forward declaration
class ClassB;
class ClassA {
public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
private:
int numA;
class ClassB {
public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}
private:
int numB;
int main() {
ClassA objectA;
ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
return 0;
}
OUTPUT:
Sum: 13
int main() {
// first function call
displayNum(5);
displayNum(8);
return 0;
}
OUTPUT:
5
8
666
9. Write a program to pass an object as a functions argument – pass object by value, pass
object by reference
i) C++ Pass Objects to Function
#include <iostream.h>
using namespace std;
class Student {
public:
double marks;
int main() {
Student student1(88.0), student2(56.0);
return 0;
}
OUTPUT:
Average Marks = 72
ii) C++ Return Object from a Function
#include <iostream.h>
using namespace std;
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 23
OBJECT ORIENTED PROGRAMMING THROUGH C++ LAB COMPUTER SCIENCE &
ENGINEERING
class Student {
public:
double marks1, marks2;
};
return student;
}
int main() {
Student student1;
// Call function
student1 = createStudent();
return 0;
}
OUTPUT:
Marks1 = 96.5
Marks2 = 75
10. Write a program to demonstrate the use of operator overloading on unary operator &
binary operators like ++ operator and << operator.
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1.display();
return 0;
}
OUTPUT:
Count: 6
Count: 7
ii) C++ Binary Operator Overloading
// C++ program to overload the binary operator +
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 26
OBJECT ORIENTED PROGRAMMING THROUGH C++ LAB COMPUTER SCIENCE &
ENGINEERING
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
void output() {
if (imag < 0)
cout << "Output Complex number: " << real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};
int main() {
Complex complex1, complex2, result;
return 0;
}
OUTPUT:
Enter first complex number:
Enter real and imaginary parts respectively: 9 5
Enter second complex number:
Enter real and imaginary parts respectively: 7 6
Output Complex number: 16+11i
#include<iostream.h>
usingnamespacestd;
classprintData
{
public:
voidprint(int i)
{
cout<<"Printing int: "<< i <<endl;
}
voidprint(double f)
{
cout<<"Printing float: "<< f <<endl;
}
voidprint(char*c)
{
cout<<"Printing string: "<< c
<<endl;
}
};
int main(void)
{
printDatapd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return0;
}
Output:
Printingint:5
Printingfloat:500.263
Printing string:Hello C++
int main() {
int a = 5;
double b = 5.5;
display(a, b);
return 0;
}
OUTPUT:
Integer number: 5
Float number: 5.5
Integer number: 5 and double number: 5.5
class Employee
{
int id;
char name[30];
public:
// Declaration of function
void getdata();
// Declaration of function
void putdata();
};
// Driver code
int main()
{
// This is an array of objects having
// maximum limit of 30 Employees
Employee emp[30];
int n, i;
cout << "Enter Number of Employees - ";
cin >> n;
class ShopItem
{
int id;
float price;
public:
void setData(int a, float b){
id = a;
price = b;
}
void getData(void){
cout<<"Code of this item is "<< id<<endl;
cout<<"Price of this item is "<<price<<endl;
}
};
int main()
{
int size = 3;
ShopItem *ptr = new ShopItem [size];
ShopItem *ptrTemp = ptr;
int p, i;
float q;
for (i = 0; i < size; i++)
{
cout<<"Enter Id and price of item "<< i+1<<endl;
cin>>p>>q;
// (*ptr).setData(p, q);
ptr->setData(p, q);
ptr++;
}
return 0;
}
OUTPUT:
class Student {
private:
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 35
OBJECT ORIENTED PROGRAMMING THROUGH C++ LAB COMPUTER SCIENCE &
ENGINEERING
int age;
public:
// constructor initializes age to 12
Student() : age(12) {}
void getAge() {
cout << "Age = " << age << endl;
}
};
int main() {
// dynamically declare Student object
Student* ptr = new Student();
return 0;
}
OUTPUT:
Age = 12
………
………
}
Program:
#include<iostream.h>
using namespace std;
class A
{
protected:
inta,b; public:
void get()
{
cout<<"Enter any two integer values"; cin>>a>>b;
}
};
class B:public A
{
int c; public:
void add()
{
c=a+b; cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
B b; b.get();
b.add();
}
Output:
Enter any two integer values1 2
1+2=3
b)Write a C++ Program that illustrate multipe inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2; public:
void get()
{
cout<<"Enter the Roll no :"; cin>>rno;
cout<<"Enter the two marks :"; cin>>m1>>m2;
}
};
class sports
{
protected:
intsm; // sm = Sports mark public:
voidgetsm()
{
cout<<"\nEnter the sports mark :"; cin>>sm;
}
};
classstatement:publicstudent,public sports
{
inttot,avg; public:
void display()
{
tot=(m1+m2+sm); avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr(); statementobj; obj.get();
obj.getsm(); obj.display(); getch();
}
Output:
Enter the Roll no: 100 Enter two marks
90
80
Enter the Sports Mark: 90 Roll No: 100
Total : 260
Average: 86.66
c) Write a C++ Program that illustrate multi level inheritance.
Program:
#include<iostream.h>
#include<conio.h>
public :
int a;
voidgetdata()
cin>>a;
voidputdata()
};
public:
int b;
void square()
getdata();
b=a*a;
cout<<"\n\nSquare Is :::"<<b;
};
public:
int c;
void cube()
square();
c=b*a;
cout<<"\n\nCube :::\t"<<c;
};
int main()
{
clrscr(); bottom b1; b1.cube();
getch();
}
Input:
Enter first number ::: 4
Output:
Square Is ::: 16 Cube ::: 64
d)Write a C++ Program that illustrate Hierarchical inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class A //Base Class
{
public:
inta,b; voidgetnumber()
{
cout<<"\n\nEnter Number :::\t"; cin>>a;
}
};
class B : public A //Derived Class 1
{
public:
void square()
{
getnumber(); //Call Base class property cout<<"\n\n\tSquare of the number :::\t"<<(a*a); cout<<"\
n\n\t -------------------------------------------------- ";
}
};
class C :public A //Derived Class 2
{
public:
void cube()
{
getnumber(); //Call Base class property cout<<"\n\n\tCube of the number :::\t"<<(a*a*a);
cout<<"\n\n\t -------------------------------------------------- ";
}
};
int main()
{
clrscr();
B b1; //b1 is object of Derived class 1 b1.square(); //call member function of class B C c1; //c1 is
class A {
public:
void show()
{
cout << "Hello from A \n";
}
};
int main()
{
D object;
object.show();
}
OUTPUT:
Hello from A
class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
return 0;
}
OUTPUT:
Derived Function
#include <iostream>
using namespace std;
public:
Calculator(T n1, T n2) {
num1 = n1;
num2 = n2;
}
void displayResult() {
cout << "Numbers: " << num1 << " and " << num2 << "." << endl;
cout << num1 << " + " << num2 << " = " << add() << endl;
cout << num1 << " - " << num2 << " = " << subtract() << endl;
cout << num1 << " * " << num2 << " = " << multiply() << endl;
cout << num1 << " / " << num2 << " = " << divide() << endl;
}
int main() {
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
return 0;
}
OUTPUT:
Int results:
Numbers: 2 and 1.
2 + 1 = 3
2 - 1 = 1
2 * 1 = 2
2 / 1 = 2
Float results:
Numbers: 2.4 and 1.2.
2.4 + 1.2 = 3.6
2.4 - 1.2 = 1.2
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 45
OBJECT ORIENTED PROGRAMMING THROUGH C++ LAB COMPUTER SCIENCE &
ENGINEERING
#include <iostream>
using namespace std;
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;
return 0;
}
OUTPUT:
2 + 3 = 5
2.2 + 3.3 = 5.5