Unit 2 OODP Sample Programs
Unit 2 OODP Sample Programs
#include<iostream>
class Point
private:
int x, y;
public:
};
int main()
Point p2 = p1;
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
OUTPUT
#include <bits/stdc++.h>
class Geeks
{
public:
void func(int x)
void func(double x)
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
3. #include <iostream>
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 v;
v=3.14*r*r*(h/3);
return v;
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);
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>
class Distance {
private:
int feet;
public:
Distance(int f)
feet = f;
void displayDistance()
};
int main() {
Distance D1(11);
return 0;
OUTPUT
First Distance : F: 11
#include <iostream>
class Distance {
private:
int feet;
public:
Distance(int f)
{feet = f;}
{ feet = D.feet;}
void displayDistance() {
};
int main() {
D1.displayDistance();
D2.displayDistance();
D1 = D2;
D1.displayDistance();
return 0;
OUTPUT
First Distance : F: 11
#include <iostream>
class Distance {
private:
int feet;
public:
Distance(int f)
feet = f;
void displayDistance() {
Distance operator- ()
feet = -feet;
return Distance(feet);
};
int main() {
Distance D1(11);
-D1;
D1.displayDistance();
return 0;
OUTPUT
F: -11
#include <iostream>
class cube {
public:
double getVolume(void)
side=s;
cube c1;
return c1;
};
int main() {
cube a;
cube b;
cube c;
a.setside(2.0);
b.setside(3.0);
volume = a.getVolume();
volume = b.getVolume();
volume = c.getVolume();
return 0;
OUTPUT
Volume of cube1 : 8
Volume of cube2 : 27