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

Unit 2 OODP Sample Programs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Unit 2 OODP Sample Programs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Copy Constructor //SYNTAX// ClassName (const ClassName &old_obj);

#include<iostream>

using namespace std;

class Point

private:

int x, y;

public:

Point(int x1, int y1) { x = x1; y = y1; }

Point(const Point &q) {x = q.x; y = q.y; }

int getX() { return x; }

int getY() { return y; }

};

int main()

Point p1(10, 15);

Point p2 = p1;

cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();

return 0;

OUTPUT

p1.x = 10, p1.y = 15

p2.x = 10, p2.y = 15

2.COMPILE TIME POLYMORPHISM-FUNCTION OVERLOADING

#include <bits/stdc++.h>

using namespace std;

class Geeks

{
public:

void func(int x)

cout << "value of x is " << x << endl;

void func(double x)

cout << "value of x is " << x << endl;

// function with same name and 2 int parameters

void func(int x, int y)

cout << "value of x and y is " << x << ", " << y << endl;

};

int main() {

Geeks obj1;

int s;

obj1.func(7);

obj1.func(9.132);

obj1.func(85,64);

return 0;

OUTPUT

value of x is 7

value of x is 9.132

value of x and y is 85, 64

3. #include <iostream>

using namespace std;

class calculate
{

public:

int radius,radius_s,l,b,h;

int volume(int r)

{ int v;

v=(4/3)*3.14*r*r*r;

return v;

int volume(int r,int h)

int v;

v=3.14*r*r*(h/3);

return v;

int volume(int l,int b,int h)

int v;

v=l*b*h;

return v;

};

int main()

calculate c;

int v;

v=c.volume(3);

cout<<"volume of Sphere="<<v<<endl;

v=c.volume(3,5);

cout<<"\n volume of Cylinder="<<v<<endl;

v=c.volume(4,5,6);
cout<<"volume of box="<<v<<endl;

return 0;

Output

volume of Sphere=84

volume of Cylinder=28

volume of box=120

4. Write a C++ Program to enter distance in feet and assign a value to variable feet using
constructor.

#include <iostream>

using namespace std;

class Distance {

private:

int feet;

public:

Distance(int f)

feet = f;

void displayDistance()

cout << "F: " << feet << endl;

};

int main() {

Distance D1(11);

cout << "First Distance : ";


D1.displayDistance();

return 0;

OUTPUT

First Distance : F: 11

5. OPERATOR OVERLAODING (ASSIGNMENT OPERATOR) PROGRAM

#include <iostream>

using namespace std;

class Distance {

private:

int feet;

public:

Distance(int f)

{feet = f;}

void operator = (const Distance &D )

{ feet = D.feet;}

void displayDistance() {

cout << "F: " << feet << endl;

};

int main() {

Distance D1(11), D2(5);

cout << "First Distance : ";

D1.displayDistance();

cout << "Second Distance :";

D2.displayDistance();

// use assignment operator

D1 = D2;

cout << "First Distance :";

D1.displayDistance();
return 0;

OUTPUT

First Distance : F: 11

Second Distance :F: 5

First Distance :F: 5

6.UNARY OPERATOR OVERLAODING(-) PROGRAM

#include <iostream>

using namespace std;

class Distance {

private:

int feet;

public:

Distance(int f)

feet = f;

void displayDistance() {

cout << "F: " << feet <<endl;

// overloaded minus (-) operator

Distance operator- ()

feet = -feet;

return Distance(feet);

};

int main() {
Distance D1(11);

-D1;

D1.displayDistance();

return 0;

OUTPUT

F: -11

7.BINARY OPERATOR OVERLAODING

#include <iostream>

using namespace std;

class cube {

double side; // Length of a cube

public:

double getVolume(void)

return side * side * side;

void setside( double s)

side=s;

// Overload + operator to add two cube objects.

cube operator+(const cube& c) {

cube c1;

c1.side = this->side +c.side;

return c1;

};

// Main function for the program

int main() {
cube a;

cube b;

cube c;

double volume = 0.0;

a.setside(2.0);

b.setside(3.0);

volume = a.getVolume();

cout << "Volume of cube1 : " << volume <<endl;

volume = b.getVolume();

cout << "Volume of cube2 : " << volume <<endl;

c = a+b; //opeartor overloading

volume = c.getVolume();

cout << "Volume of cube3 : " << volume <<endl;

return 0;

OUTPUT

Volume of cube1 : 8

Volume of cube2 : 27

Volume of cube3 : 125

You might also like