Oodp Practical File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

SRM INSTITUTE OF SCIENCE & TECHNOLOGY

Delhi-NCR Campus, Modinagar , Ghaziabad-201204

PRACTICAL FILE

SUBJECT TITLE : OBJECT ORIENTED DESIGN AND PROGRAMING

SUBJECT CODE : 21CSC101T

NAME:

REGISTRATION NO.

DEGREE:

BRANCH & SECTION:

SEMESTER AND YEAR:


SRM INSTITUTE OF SCIENCE & TECHNOLOGY
(under section 3 of UGC Act, 1956)

BONAFIDE CERTIFICATE

It is to be certified that the bonafide record submitted by NAME ___________

& Reg.No. ________________ of FIRST year & SECOND semester for Bachelor

of Technology in Bachelor Degree in the Department of Computer Science &

Engineering, SRM IST has been done for the course Object Oriented Design

& Programing Lab during the academic year of 2023-24.

LAB IN CHARGE: HEAD OF DEPARTMENT:

EXANINER-1 EXAMINER-2
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
DELHI NCR CAMPUS, MODINAGAR

LIST OF EXPERIMENTS

Exp. No. Title of Experiments Date Signature


1 Write a program for input/output operators in C++.

2 Write a program for data type in C++.

3 Write a program for operators in C++.

4 Write a program for decision making in C++.

5 Write a program for loops in C++.

6 Write a program for class and object in C++.

7 Write a program for constructor& destructor in C++.

8 Write a program for operator overloading in C++.

9 Write a program for function overloading in C++.

10 Write a program for inheritance in C++.

11 Write a program for friend function in C++.


Write a program for inline function in C++.
12
Write a program for virtual function in C++.
13

VALUE ADDED PROGRAMS


Exp.No Programs Date Signature
1 Write a program for template in C++.

2 Write a program for exception handling in C++.


Program-1: Write A Program for input/output operators in C++.
#include <iostream>
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

Program-2: Write a Program for Data Type in C++.

#include<iostream>
using namespace std;
int main()
{
int myNum = 5;
float myFloatNum = 5.99;
double myDoubleNum = 9.98908;
char myLetter = 'D';
string myText = "Hello";
cout<<mynum<<endl<< myFloatNum<<endl<< myDoubleNum<<endl<< myLetter<<endl<<
myText; return 0;
}
OUTPUT:
5
5.99
9.98908
D
Hello

Program-3: Write a program for operators in C++.

#include <iostream>
using namespace std;
int main() {
int a, b;
a = 7;
b = 2;
cout << "a + b = " << (a + b) <<
endl; cout << "a - b = " << (a - b) <<
endl; cout << "a * b = " << (a * b)
<< endl; cout << "a / b = " << (a / b)
<< endl; cout << "a % b = " << (a %
b) << endl; return 0;
}
OUTPUT:

a+b=9
a-b= 5
a * b = 14
a/b=3
a%b=1
Program-4: Write a program for if_else statement in C++.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
return 0;
}
OUTPUT:

Enter an integer: 4
You entered a positive integer: 4
Enter an integer: -4
You entered a negative integer: -4

Program-5: Write a program for loops in C++.


#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; ++i) {
cout << "Hello World! " << endl;
}
return 0;
}
OUTPUT:
HelloWorld!
HelloWorld!
HelloWorld!
Program-6: Write a program for class & objects in C++.
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:

Rectangle(double l, double w) : length(l), width(w)

{} double area() {
return length * width;
}

};
int main() {
Rectangle rect(5.0, 3.0);
cout << “Area of the rectangle: ” << rect.area() <<
endl; return 0;
}
OUTPUT:

Area of the rectangle: 15

Program-7: Write a program for Constructor & Destructor in C++.

#include <iostream>
using namespace std;
class Z
{
public:
Z()
{
cout<<"Constructor called"<<endl;
}
~Z()
{
cout<<"Destructor called"<<endl;
}
};
int main()
{
Z z1;
int a = 1;
if(a==1)
{
Z z2;
}
}
OUTPUT:

Constructor called
Constructor called
Destructor called
Destructor called

Program-8: Write a program for operator overloading in C++.

#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() : value(5)
{} void operator ++
() {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
++count1;
count1.display();
return 0;
OUTPUT:

Count: 6

Program-9: Write a program for function overloading in C++.


#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
OUTPUT:

sum = 12
sum = 11.5

Program-10: Write a program for inheritance in C++.


#include <iostream>
using namespace std;
class Person {
int id;
char name[100];
public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}

};

class Student : private Person {


char course[50];
int fee;
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s()
; return 0;}

OUTPUT:

Enter the Id:054


Enter the Name: vaibhav
Enter the Course Name: B.tech
Enter the Course Fee:300000

Id: 54
Name: vaibhav
Course: B.tech
Fee: 300000
Program 11: Write a program for friend function in C++

#include <iostream>

using namespace std;

class Distance

{ private:

int meter;

friend int addFive(Distance);

public:

Distance() : meter(0) {}

};

int addFive(Distance d)

{ d.meter += 5;

return d.meter;

int main() {

Distance

D;

cout << "Distance: " <<

addFive(D); return 0;

Output:

Distance: 5
Program12: Write a program for inline function in C++.

#include <iostream>

using namespace std;

class operation {

int a, b, add, sub, mul;

float div;

public:

void get();

void sum();

void difference();

void product();

void division();

};

inline void operation ::get()

cout << "Enter first

value:"; cin >> a;

cout << "Enter second value:";

cin >> b;

inline void operation ::sum()

add = a + b;

cout << "Addition of two numbers: " << a + b << "\n";

inline void operation ::difference()

sub = a - b;
cout << "Difference of two numbers: " << a - b << "\n";

inline void operation ::product()

mul = a * b;

cout << "Product of two numbers: " << a * b << "\n";

}
inline void operation::division()

div = a / b;

cout << "Division of two numbers: " << a / b << "\n";

int main()

cout << "Program using inline function\n";

operation s;

s.get();

s.sum();

s.difference();

s.product();

s.division()

; return 0;

Output:

Program using inline function

Enter first value:2

Enter second value:3


Addition of two numbers: 5

Difference of two numbers: -1

Product of two numbers: 6

Division of two numbers: 0

Program13: Write a program for virtual and pure virtual function in C++.

1. Virtual Function

#include <iostream>

using namespace std;

class base

public:

void show()

cout << "Base class" << std::endl;

};

class derived1 : public base

public:

void show()

cout << "Derived class 1" << std::endl;

};

class derived2 : public base

public:

void show()
{
std::cout << "Derived class 2" << std::endl;

};

int main()

base *b;

derived1 d1;

derived2 d2;

b=&d1;

b->show();

b=&d2;

b->show();

return 0;

Output:

Base class

Base class

2. Pure virtual

function #include

<iostream> using

namespace std;

// Abstract class

class Shape

public:

virtual float calculateArea() = 0; // pure virtual function.


};

class Square : public Shape

float a;

public:

Square(float l)

a = l;

float calculateArea()

return a*a;

};

class Circle : public Shape

float r;

public:

Circle(float x)

r = x;

float calculateArea()

{
return 3.14*r*r ;

};

class Rectangle : public Shape

float l;

float b;

public:

Rectangle(float x, float y)

l=x;

b=y

float calculateArea()

return l*b;

};

int main()

Shape *shape;

Square s(3.4);

Rectangle
r(5,6);

Circle c(7.8);
shape =&s;

int a1 =shape->calculateArea();

shape = &r;

int a2 = shape->calculateArea();

shape = &c;

int a3 = shape->calculateArea();

cout << "Area of the square is " <<a1<< std::endl;

cout << "Area of the rectangle is " <<a2<<

std::endl; cout << "Area of the circle is " <<a3<<

std::endl; return 0;

Output:

Area of the square is 11

Area of the rectangle is 30

Area of the circle is 191


VALUE ADDED PROGRAMS
Program 1: Write a program for template in C++.

#include <iostream>

using namespace std;

template <typename T> T myMax(T x, T y)

return (x > y) ? x : y;

int main()
{

cout << myMax<int>(3, 7) << endl;

cout << myMax<double>(3.0, 7.0) <<

endl; cout << myMax<char>('g', 'e') <<

endl;

return 0;

Output:

g
Program-2: Write a program for exception handling in C++.

#include <iostream>

using namespace std;

int main() {

double numerator, denominator, divide;

cout << "Enter numerator: ";

cin >> numerator;

cout << "Enter denominator: ";

cin >> denominator;

try {

if (denominator == 0)

throw 0;

divide = numerator / denominator;

cout << numerator << " / " << denominator << " = " << divide << endl;
}

catch (int num_exception) {

cout << "Error: Cannot divide by " << num_exception << endl;

return 0;

Output:

Enter numerator: 2

Enter denominator: 4

2 / 4 = 0.5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy