Function Overloading
Function Overloading
#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;
}
Output
Integer number: 5
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
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
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:
class Area
{
private:
int length;
int breadth;
public:
// Constructor with no arguments
Area(): length(5), breadth(2) { }
void GetLength()
{
cout << "Enter length and breadth respectively:
";
cin >> length >> breadth;
}
int main()
{
Area A1, A2(2, 1);
int 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";
}
strcpy(name, a.name);
strcat(name, b.name);
}
};
int main ()
{
char *first = "Hello !";
dynamic name1(first);
dynamic name2("Technology");
dynamic name3("Lovers");
name1.show();
name2.show();
name3.show();
return 0;
}
Program -8
#include <iostream>
public:
private:
double length;
};
Line::Line(void) {
Line::~Line(void) {
length = len;
int main() {
Line line;
line.setLength(6.0);
return 0;
#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);
};
void main() {
clrscr();
base obj;
obj.get();
cout << "\n Mean value is : " << mean(obj);
getch();
}
Sample Output
#include <iostream>
using namespace std;
class Weapon
{
public:
void loadFeatures()
{ cout << "Loading weapon 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