You are on page 1of 22

Programs – Unit – I & II

// Inline functions

#include <iostream>

using namespace std;

inline float mul(float x, float y)


{
return(x*y);
}

inline double div(double p, double q)


{
return(p/q);
}

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>

using namespace std;

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;
}

float value(float p, int n, float r)


{
int year = 1;
float sum = p;
while(year <= n)
{
sum = sum * (1 + r);
year = year + 1;
}
return(sum);
}

void printline(char ch, int len)


{
for(int i = 1; i <= len; i++)
cout << ch;
cout << "\n";
}

OUTPUT

****************************************
Final value = 10056.8
========================================

2 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


// Function overloading
// Function volume() is overloaded three times

#include <iostream>

using namespace std;

// 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

int volume(int s) // cube


{
return (s * s * s);
}

double volume(double r, int h) // cylinder


{
return (3.14519 * r * r * h);
}

long volume(long l, int b, int h) // rectangular box


{
return (l * b * h);
}

OUTPUT

1000
157.26
112500

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 3


/* C++ program to demonstrate methods of passing arguments in function.
* Pass by value, Pass by reference, Pass by address.
*/

#include <iostream>

using namespace std;

void swapByValue(int a, int b);


void swapByRef(int &a, int &b);
void swapByAdr(int *a, int *b);

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;
}

void swapByValue(int a, int b)


{
int c;
c = a;
a = b;
b = c;
}

4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


void swapByRef(int &a, int &b)
{
int c;
c = a;
a = b;
b = c;
}

void swapByAdr(int *a, int *b )


{
int c;
c = *a;
*a = *b;
*b = c;
}

OUTPUT

Value before Swapping :


x = 10 y = 20
Value after Swapping :
x = 10 y = 20
Value before Swapping :
x = 10 y = 20
Value after Swapping :
x = 20 y = 10
Value before Swapping :
x = 10 y = 20
Value after Swapping :
x = 20 y = 10

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 5


// Class implementation

#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";
}
};

// Member function definition

void Item :: getdata(int a, float b) // use membership label


{
number = a; // private variables
cost = b; // directly used
}

// 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

6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


/* C++ program to create class for a student. */
#include <iostream>
using namespace std;

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);
};

// member function definition, outside of the class


void Student :: getDetails(void){
cout << "Enter roll no. : ";
cin >> rollNo;
cout << "Enter name : " ;
cin >> name;
cout << "Enter total marks out of 500 : ";
cin >> total;
perc = (float) total / 500 * 100;
}

// member function definition, outside of the class


void Student :: putDetails(void){
cout << "Student Details :\n";
cout << "Roll no. : " << rollNo << "\n";
cout << "Name : " << name << "\n";
cout << "Total : " << total << "\n";
cout << "Percentage : " << perc << "\n";
}

int main()
{
Student std; //object creation
std.getDetails();
std.putDetails();
return 0;
}

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 7


OUTPUT

Enter roll no. : 102


Enter name : B.Bhuvaneswaran
Enter total marks out of 500 : 333
Student Details :
Roll no. : 102
Name : B.Bhuvaneswaran
Total : 333
Percentage : 66.6

8 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


/* C++ program to create student class,
* read and print N student's details
(Example of array of objects). */
#include <iostream>
using namespace std;

#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);
};

// member function definition, outside of the class


void Student :: getDetails(void){
cout << "Enter roll no. : ";
cin >> rollNo;
cout << "Enter name : " ;
cin >> name;
cout << "Enter total marks out of 500 : ";
cin >> total;
perc = (float) total / 500 * 100;
}

// member function definition, outside of the class


void Student :: putDetails(void){
cout << "Student Details :\n";
cout << "Roll no. : " << rollNo << "\n";
cout << "Name : " << name << "\n";
cout << "Total : " << total << "\n";
cout << "Percentage : " << perc << "\n";
}

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";

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 9


std[i].getDetails();
}
for(i = 0; i < n; i++)
{
cout << "Details of student " << i + 1 << " :\n";
std[i].putDetails();
}
return 0;
}

OUTPUT

Enter total number of students : 2


Enter details of student 1 :
Enter roll no. : 101
Enter name : B.Bhuvaneswaran
Enter total marks out of 500 : 333
Enter details of student 2 :
Enter roll no. : 102
Enter name : S.Chithra
Enter total marks out of 500 : 424
Details of student 1 :
Student Details :
Roll no. : 101
Name : B.Bhuvaneswaran
Total : 333
Percentage : 66.6
Details of student 2 :
Student Details :
Roll no. : 102
Name : S.Chithra
Total : 424
Percentage : 84.8

10 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


/* C++ program to create class for an Employee */

#include <iostream>

using namespace std;

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);
};

// member function definition, outside of the class


void Employee :: getDetails(void)
{
cout << "Enter employee id : ";
cin >> empid;
cout << "Enter name : " ;
cin >> name;
cout << "Enter designation : " ;
cin >> desig;
cout << "Enter department : " ;
cin >> dept;
cout << "Enter salary : ";
cin >> salary;
}

// member function definition, outside of the class


void Employee :: putDetails(void)
{
cout << "Employee Details :\n";
cout << "Employee Id. :"<< empid << "\n";
cout << "Name :"<< name << "\n";
cout << "Designation :"<< desig << "\n";
cout << "Department :"<< dept << "\n";
cout << "Salary :"<< salary << "\n";
}

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 11


int main()
{
Employee emp; //object creation
emp.getDetails();
emp.putDetails();
return 0;
}

OUTPUT

Enter employee id : 101


Enter name : B.Bhuvaneswaran
Enter designation : AP
Enter department : CSE
Enter salary : 45000
Employee Details :
Employee Id. :101
Name :B.Bhuvaneswaran
Designation :AP
Department :CSE
Salary :45000

12 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


/* C++ program to create Employee class,
* read and print N Employee's details
* (Example of array of objects). */

#include <iostream>

using namespace std;

#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);
};

// member function definition, outside of the class


void Employee :: getDetails(void)
{
cout << "Enter employee id : ";
cin >> empid;
cout << "Enter name : " ;
cin >> name;
cout << "Enter designation : " ;
cin >> desig;
cout << "Enter department : " ;
cin >> dept;
cout << "Enter salary : ";
cin >> salary;
}

// member function definition, outside of the class


void Employee :: putDetails(void)
{
cout << "Employee Details :\n";
cout << "Employee Id. :"<< empid << "\n";
cout << "Name :"<< name << "\n";
cout << "Designation :"<< desig << "\n";
cout << "Department :"<< dept << "\n";
cout << "Salary :"<< salary << "\n";
}

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 13


int main()
{
Employee emp[MAX]; // arrays of object creation
int n, i;
cout << "Enter total number of employees : ";
cin >> n;
for(i = 0; i < n; i++)
{
cout << "Enter details of employee " << i + 1 << " :\n";
emp[i].getDetails();
}
for(i = 0; i < n; i++)
{
cout << "Details of employee " << i + 1 << " :\n";
emp[i].putDetails();
}
return 0;
}

OUTPUT

Enter total number of employees : 2


Enter details of employee 1 :
Enter employee id : 101
Enter name : B.Bhuvaneswaran
Enter designation : AP
Enter department : CSE
Enter salary : 45000
Enter details of employee 2 :
Enter employee id : 102
Enter name : S.Chithra
Enter designation : AP
Enter department : CSE
Enter salary : 36000
Details of employee 1 :
Employee Details:
Employee Id. :101
Name :B.Bhuvaneswaran
Designation :AP
Department :CSE
Salary :45000
Details of employee 2 :
Employee Details:
Employee Id. :102
Name :S.Chithra
Designation :AP
Department :CSE
Salary :36000

14 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


/* C++ program to create class to read and add two times. */

#include <iostream>

using namespace std;

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";
}

void Time::addTime(Time T1,Time T2)


{

seconds = T1.seconds + T2.seconds;


minutes = T1.minutes + T2.minutes + seconds / 60;;
hours = T1.hours + T2.hours + (minutes / 60);
minutes = minutes % 60;
seconds = seconds % 60;
}

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 15


int main()
{
Time T1, T2, T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1, T2);
T3.putTime();
return 0;
}

OUTPUT

Enter time:
Hours : 14
Minutes : 6
Seconds : 11
Enter time:
Hours : 2
Minutes : 11
Seconds : 15
Time after add : 16:17:26

16 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


/* C++ program for unary minus (-) operator overloading. */

#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

Value of n is: -10

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 17


/* C++ program for unary increment (++) and decrement (--) operator
overloading. */

#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

After increment - Value of n is : 11


After decrement - Value of n is : 10

18 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


/* C++ program for unary logical NOT (!) operator overloading. */

#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

Before calling Operator Overloading : Value of n is : 10


After calling Operator Overloading : Value of n is : 0

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 19


/* C++ program to add two objects using binary plus (+) operator
overloading. */

#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);

//add two objects


sum = num1 + num2;
sum.dispNum();
cout << "\n";
return 0;
}

OUTPUT

Number is: 30

20 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


/* C++ program to add two distances using binary plus (+) operator
overloading. */

#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;
}

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 21


OUTPUT

Enter the first distance :


Enter feet : 14
Enter inches : 6
Enter second distance :
Enter feet : 2
Enter inches : 11
Total Distance :
Feet : 17 Inches : 5

22 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai

You might also like