You are on page 1of 21

Program 1.

#include <iostream>
using namespace std;

void display(int);
void display(float);
void display(int, float);

int main() {

int a = 5;
float b = 5.5;

display(a);
display(b);
display(a, b);

return 0;
}

void display(int var) {


cout << "Integer number: " << var << endl;
}

void display(float var) {


cout << "Float number: " << var << endl;
}
void display(int var1, float var2) {
cout << "Integer number: " << var1;
cout << " and float number:" << var2;
}

Output

Integer number: 5

Float number: 5.5

Integer number: 5 and float number: 5.5


Program -2
#include<iostream>
using namespace std;

class circle
{
float radius, area; //data members
public:
circle()
{
cout<<"\n Enter the value of Radius : ";
cin>>radius;
}
void calculate(); //member function
void display(); //member function
};
inline void circle :: calculate() //accessing data members of a class circle
{
area = 3.14 * radius * radius;
}
inline void circle :: display()
{
cout<<"\n Area of Circle : "<<area;
}
int main()
{
circle cr; //object created
cr.calculate(); //calling function
cr.display(); //calling function
return 0;
}

Output:
Program -3

1. /*
2. * C Program to Find the Volume and Surface Area of
cylinder
3. */
4. #include <stdio.h>
5. #include <math.h>
6.
7. int main()
8. {
9.
10. float radius, height;
11. float surface_area, volume;
12.
13. printf("Enter value for radius and height
of a cylinder : \n");
14. scanf("%f%f", &radius, &height);
15. surface_area = 2 * (22 / 7) * radius *
(radius + height);
16. volume = (22 / 7) * radius * radius *
height;
17. printf("Surface area of cylinder is: %.3f",
surface_area);
18. printf("\n Volume of cylinder is : %.3f",
volume);
19. return 0;
20. }

Output:
$ cc pgm29.c -lm
$ a.out
Enter value for radius and height of a cylinder :
15 17
Surface area of cylinder is: 2880.000
Volume of cylinder is : 11475.000
Program -4

// Cpp program to illustrate the


// concept of Constructors
#include <iostream>
using namespace std;

class construct
{
public:
int a, b;

// Default Constructor
construct()
{
a = 10;
b = 20;
}
};

int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: "<< c.a << endl << "b: "<< c.b;
return 1;
}

Output

a: 10

b: 20
Program -5

// CPP program to illustrate


// parameterized constructors
#include<iostream>
using namespace std;

class Point
{
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}

int getX()
{
return x;
}
int getY()
{
return y;
}
};

int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX() << ", p1.y = " <<
p1.getY();

return 0;
}

Output:

p1.x = 10, p1.y = 15


Program -6

Source Code to demonstrate the working of overloaded


constructors
#include <iostream>
using namespace std;

class Area
{
private:
int length;
int breadth;

public:
// Constructor with no arguments
Area(): length(5), breadth(2) { }

// Constructor with two arguments


Area(int l, int b): length(l), breadth(b){ }

void GetLength()
{
cout << "Enter length and breadth respectively:
";
cin >> length >> breadth;
}

int AreaCalculation() { return length * breadth; }

void DisplayArea(int temp)


{
cout << "Area: " << temp << endl;
}
};

int main()
{
Area A1, A2(2, 1);
int temp;

cout << "Default Area when no argument is passed." <<


endl;
temp = A1.AreaCalculation();
A1.DisplayArea(temp);

cout << "Area when (2,1) is passed as argument." <<


endl;
temp = A2.AreaCalculation();
A2.DisplayArea(temp);

return 0;
}

Program -7
Dynamic constructor example
#include<iostream>
#include<string.h>
using namespace std;

class dynamic
{
char *name;
int length;

public :
dynamic()
// First Constructor
{
length = 0;
name = new char[length + 1];
}

dynamic(char *s)
// Second Constructor
{
length = strlen(s);
name = new char[length + 1];
strcpy(name,s);
}
void show()
// Method to display name using dynamic allocation in
join method
{
cout << name << "\n";
cout << "Number of characters in the string is " <<
strlen(name) << "\n\n";
}

void join(dynamic &a, dynamic &b)


{
length = a.length + b.length;
delete name;
name = new char[length + 1];
// dynamic allocation of name using new

strcpy(name, a.name);
strcat(name, b.name);
}
};

int main ()
{
char *first = "Hello !";
dynamic name1(first);
dynamic name2("Technology");
dynamic name3("Lovers");

dynamic s1, s2;


s1.join(name1, name2);
s2.join(s1, name3);

name1.show();
name2.show();
name3.show();

return 0;
}

Program -8

Program to implement destructor

#include <iostream>

using namespace std;


class Line {

public:

void setLength( double len );

double getLength( void );

Line(); // This is the constructor declaration

~Line(); // This is the destructor: declaration

private:

double length;

};

// Member functions definitions including constructor

Line::Line(void) {

cout << "Object is being created" << endl;

Line::~Line(void) {

cout << "Object is being deleted" << endl;

void Line::setLength( double len ) {

length = len;

double Line::getLength( void ) {


return length;

// Main function for the program

int main() {

Line line;

// set line length

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

return 0;

When the above code is compiled and executed, it produces the


following result −
Object is being created
Length of l
Object
ine : 6 is being deleted
Program -9

Simple Program for Friend Function

#include<iostream.h>
#include<conio.h>

class base {
int val1, val2;
public:

void get() {
cout << "Enter two values:";
cin >> val1>>val2;
}
friend float mean(base ob);
};

float mean(base ob) {


return float(ob.val1 + ob.val2) / 2;
}

void main() {
clrscr();
base obj;
obj.get();
cout << "\n Mean value is : " << mean(obj);
getch();
}

Sample Output

Enter two values: 10, 20


Mean Value is: 15
Program -10

Program to difine virtual function

#include <iostream>
using namespace std;

class Weapon
{
public:
void loadFeatures()
{ cout << "Loading weapon features.\n"; }
};

class Bomb : public Weapon


{
public:
void loadFeatures()
{ cout << "Loading bomb features.\n"; }
};

class Gun : public Weapon


{
public:
void loadFeatures()
{ cout << "Loading gun features.\n"; }
};

int main()
{
Weapon *w = new Weapon;
Bomb *b = new Bomb;
Gun *g = new Gun;

w->loadFeatures();
b->loadFeatures();
g->loadFeatures();

return 0;
}

Output

Loading weapon features.

Loading bomb features.

Loading gun features.

You might also like