Professional Documents
Culture Documents
// Inline functions
#include <iostream>
int main()
{
float a = 12.345;
float b = 9.82;
cout << mul(a,b) << "\n";
cout << div(a,b) << "\n";
return 0;
}
OUTPUT
121.228
1.25713
// Default arguments
#include <iostream>
int main()
{
float amount;
float value(float p, int n, float r=0.15); //prototype
void printline(char ch='*', int len=40); //prototype
printline(); //used default values for arguments
amount = value(5000.00,5); //default for 3rd argument
cout << "Final value = " << amount << "\n";
printline('='); //use default value for 2nd argument
return 0;
}
OUTPUT
****************************************
Final value = 10056.8
========================================
#include <iostream>
// Declarations (prototypes)
int volume(int);
double volume(double, int);
long volume(long, int, int);
int main()
{
cout << volume(10) << "\n";
cout << volume(2.5, 8) << "\n";
cout << volume(100L, 75, 15) << "\n";
return 0;
}
// Function definitions
OUTPUT
1000
157.26
112500
#include <iostream>
int main()
{
int x, y;
x = 10;
y = 20;
cout << "Value before Swapping :\n";
cout << "x = " << x << " y = " << y << "\n";
/* In call by value swapping does not reflect in calling function */
swapByValue(x, y);
cout << "Value after Swapping :\n";
cout << "x = " << x << " y = " << y << "\n";
x = 10;
y = 20;
cout << "Value before Swapping :\n";
cout << "x = " << x << " y = " << y << "\n";
/* Swapping reflect but reference does not take space in memory */
swapByRef(x, y);
cout << "Value after Swapping :\n";
cout << "x = " << x << " y = " << y << "\n";
x = 10;
y = 20;
cout << "Value before Swapping :\n";
cout << "x = " << x << " y = " << y << "\n";
/* Swapping reflect but pointer takes space in memory */
swapByAdr(&x, &y);
cout << "Value after Swapping :\n";
cout << "x = " << x << " y = " << y << "\n";
return 0;
}
OUTPUT
#include <iostream>
using namespace std;
class Item
{
int number; // private by default
float cost; // private by default
public:
void getdata(int a, float b); // prototype declaration,
// to be defined
// Function defined inside the class
void putdata(void)
{
cout << "Number :" << number << "\n";
cout << "Cost :" << cost << "\n";
}
};
// Main program
int main()
{
Item x; // create object x
cout << "Object x " << "\n";
x.getdata(100, 299.95); // call member function
x.putdata(); // call member function
Item y; // create another object
cout << "Object y " << "\n";
y.getdata(200, 175.50);
y.putdata();
return 0;
}
OUTPUT
Object x
Number :100
Cost :299.95
Object y
Number :200
Cost :175.5
class Student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
// member function to get student's details
void getDetails(void);
// member function to print student's details
void putDetails(void);
};
int main()
{
Student std; //object creation
std.getDetails();
std.putDetails();
return 0;
}
#define MAX 10
class Student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
// member function to get student's details
void getDetails(void);
// member function to print student's details
void putDetails(void);
};
int main()
{
Student std[MAX]; // array of objects creation
int n, i;
cout << "Enter total number of students : ";
cin >> n;
for(i = 0; i < n; i++)
{
cout << "Enter details of student " << i + 1 << " :\n";
OUTPUT
#include <iostream>
class Employee
{
private:
int empid;
char name[30];
char desig[20];
char dept[10];
float salary;
public:
// member function to get employee's details
void getDetails(void);
//member function to print employee's details
void putDetails(void);
};
OUTPUT
#include <iostream>
#define MAX 10
class Employee
{
private:
int empid;
char name[30];
char desig[20];
char dept[10];
float salary;
public:
// member function to get employee's details
void getDetails(void);
//member function to print employee's details
void putDetails(void);
};
OUTPUT
#include <iostream>
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
void getTime(void);
void putTime(void);
void addTime(Time T1,Time T2);
};
void Time::getTime(void)
{
cout << "Enter time:" << "\n";
cout << "Hours : ";
cin>>hours;
cout << "Minutes : ";
cin>>minutes;
cout << "Seconds : ";
cin>>seconds;
}
void Time::putTime(void)
{
cout << "Time after add : ";
cout << hours << ":" << minutes << ":" << seconds << "\n";
}
OUTPUT
Enter time:
Hours : 14
Minutes : 6
Seconds : 11
Enter time:
Hours : 2
Minutes : 11
Seconds : 15
Time after add : 16:17:26
#include<iostream>
using namespace std;
class NUM
{
private:
int n;
public:
//function to get number
void getNum(int x)
{
n = x;
}
//function to display number
void dispNum(void)
{
cout << "Value of n is: " << n;
}
//unary - operator overloading
void operator -(void)
{
n = -n;
}
};
int main()
{
NUM num;
num.getNum(10);
-num;
num.dispNum();
cout << "\n";
return 0;
}
OUTPUT
#include<iostream>
using namespace std;
class NUM
{
private:
int n;
public:
//function to get number
void getNum(int x)
{
n = x;
}
//function to display number
void dispNum(void)
{
cout << "Value of n is : " << n;
}
//unary ++ operator overloading
void operator ++(void)
{
n = ++n;
}
//unary -- operator overloading
void operator --(void)
{
n = --n;
}
};
int main()
{
NUM num;
num.getNum(10);
++num;
cout << "After increment - ";
num.dispNum();
cout << "\n";
--num;
cout << "After decrement - ";
num.dispNum();
cout << "\n";
return 0;
}
OUTPUT
#include<iostream>
using namespace std;
class NUM
{
private:
int n;
public:
//function to get number
void getNum(int x)
{
n = x;
}
//function to display number
void dispNum(void)
{
cout << "Value of n is : " << n;
}
//unary ! operator overloading
void operator !(void)
{
n = !n;
}
};
int main()
{
NUM num;
num.getNum(10);
cout << "Before calling Operator Overloading : ";
num.dispNum();
cout << "\n";
!num;
cout << "After calling Operator Overloading : ";
num.dispNum();
cout << "\n";
return 0;
}
OUTPUT
#include<iostream>
using namespace std;
class NUM
{
private:
int n;
public:
//function to get number
void getNum(int x)
{
n = x;
}
//function to display number
void dispNum(void)
{
cout << "Number is: " << n;
}
//add two objects - Binary Plus(+) Operator Overloading
NUM operator +(NUM &obj)
{
NUM x; //create another object
x.n = n + obj.n;
return (x); //return object
}
};
int main()
{
NUM num1, num2, sum;
num1.getNum(10);
num2.getNum(20);
OUTPUT
Number is: 30
#include<iostream>
using namespace std;
class Distance
{
private:
int feet, inches;
public:
//function to read distance
void readDistance(void)
{
cout << "Enter feet : ";
cin >> feet;
cout << "Enter inches : ";
cin >> inches;
}
//function to display distance
void dispDistance(void)
{
cout << "Feet : " << feet << " Inches : " << inches << "\n";
}
//add two Distance using + operator overloading
Distance operator +(Distance &dist)
{
Distance temp; //to add two distances
temp.inches = inches + dist.inches;
temp.feet = feet + dist.feet + (temp.inches / 12);
temp.inches = temp.inches % 12;
return temp;
}
};
int main()
{
Distance D1, D2, D3;
cout << "Enter the first distance :\n";
D1.readDistance();
cout << "Enter second distance : \n";
D2.readDistance();
//add two distances
D3 = D1 + D2;
cout << "Total Distance :\n";
D3.dispDistance();
cout << "\n";
return 0;
}