8/13/2019 c Practical Programs
1/56
FRIEND FUNCTION:
#include
#include
class ONE;
class TWO
{
public:
void print(ONE&x);
};
class ONE
{
int a,b;
friend void TWO::print(ONE&x);
public:
ONE():a(1),b(2)
{
}
};
void TWO::print(ONE&x)
{
cout
8/13/2019 c Practical Programs
2/56
}
int main()
{
clrscr();
ONE xobj;
TWO yobj;
yobj.print(xobj);
return 0;
getch();
}
8/13/2019 c Practical Programs
3/56
8/13/2019 c Practical Programs
4/56
8/13/2019 c Practical Programs
5/56
long sum;
sum=x+y;
return sum;
}
float add(float x,float y)
{
float sum;
sum=x+y;
return sum;
getch();
}
8/13/2019 c Practical Programs
6/56
OUTPUT:
Enter two integers
4
9
sum of integers:13
Enter two floating point numbers
4.5
8.2
sum of floats:12.7
8/13/2019 c Practical Programs
7/56
ERROR HANDLING DURING FIVE OPERATIONS:
#include
#include
int main()
{
double Operand1, Operand2, Result;
char Operator;
clrscr();
cout > Operand1 >> Operator >> Operand2;
switch(Operator)
{
case '+':
Result = Operand1 + Operand2;
break;
case '-':
Result = Operand1 - Operand2;
break;
case '*':
Result = Operand1 * Operand2;
break;
case '/':
8/13/2019 c Practical Programs
8/56
8/13/2019 c Practical Programs
9/56
8/13/2019 c Practical Programs
10/56
FORMS OF INHERITANCE:
#include
#include
class Cpolygon
{
protected:
int width, height;
public:
void input_values (int one, int two)
{
width=one;
height=two;
}
};
class Crectangle: public Cpolygon
{
public:
int area ()
{
return (width * height);
}
8/13/2019 c Practical Programs
11/56
};
class Ctriangle: public Cpolygon
{
public:
int area ()
{
return (width * height / 2);
}
};
int main ()
{
Crectangle rectangle;
Ctriangle triangle;
rectangle.input_values (2,2);
triangle.input_values (2,2);
clrscr();
cout
8/13/2019 c Practical Programs
12/56
OUTPUT:
area of rectangle:4
area of triangle:2
8/13/2019 c Practical Programs
13/56
INLINE FUNCTION:
#include
#include
inline float triangle_area(float base,float height)
{
float area;
area=(0.5*base*height);
return area;
}
int main(void)
{
float b,h,a;
b=4;
h=6;
a=triangle_area(b,h);
clrscr();
cout
8/13/2019 c Practical Programs
14/56
OUTPUT:
Area=(0.5*base*height)
Where,base=4,height=6
Area=12
8/13/2019 c Practical Programs
15/56
MANIPULATORS:
#include
#include
#include
#include
void main ( )
{
float x = 23.012345678;
float y = 44;
float z = 3.765;
float a = 4.5;
clrscr();
cout
8/13/2019 c Practical Programs
16/56
OUTPUT:
2345.012346
0.005544
23.012346
44
3.765
4.5
23.012346443.7654.5
8/13/2019 c Practical Programs
17/56
MULTI-DIMENSIONAL ARRAYS:
#include
#include
#include
int main()
{
const int Rows=4;
const int Columns=4;
int matrix[Rows][Columns];
int copyarray;
const int End_of_row=4;
const int End_of_col=4;
int r;
int c;
clrscr();
cout
8/13/2019 c Practical Programs
18/56
cin>>matrix[r][c];
for(r=0;r
8/13/2019 c Practical Programs
19/56
8/13/2019 c Practical Programs
20/56
SPECIAL OPERATORS:
#include
#include
class distance
{
int feet;
float inches;
public:
distance()
{
feet=0;
inches=0.0;
}
distance(int ft,float in)
{
feet=ft;
inches=in;
}
void getdist()
{
coutfeet;
8/13/2019 c Practical Programs
21/56
coutinches;
}
void showdist()
{
cout
8/13/2019 c Practical Programs
22/56
clrscr();
distance dist1,dist2(11,6.25),dist3,dist4;
dist1.getdist();
dist3=dist1+dist2;
dist4=dist1+dist2+dist3;
cout
8/13/2019 c Practical Programs
23/56
OUTPUT:
Enter feet:7
Enter inches:13.5
dist1=7'-13.5"
dist2=11'-6.25"
dist3=19'-7.75"
dist4=39'-3.5"
8/13/2019 c Practical Programs
24/56
STREAM CLASSES:
#include
#include
#include
class numbers
{
public:
numbers(char[]="unknown",int=0,float=0.0);
void display();
void update();
private:
char name[30];
int a;
float b;
};
numbers::numbers(char nm[],int j,float k)
{
strcpy(name,nm);
a=j;
b=k;
}
void numbers::update()
8/13/2019 c Practical Programs
25/56
{
coutb;
}
void numbers::display()
{
cout
8/13/2019 c Practical Programs
26/56
OUTPUT:
The name is unknown
The number are 0 and 0
The name is John
The number are 12345 and 678.900024
8/13/2019 c Practical Programs
27/56
PURE VIRTUAL FUNCTION:
#include
#include
class Base
{
public:
void Nonvirtual()
{
cout
8/13/2019 c Practical Programs
28/56
voidvirtual()
{
cout Nonvirtual();
bBase->voidvirtual();
bDerived->Nonvirtual();
bDerived->voidvirtual();
getch();
}
8/13/2019 c Practical Programs
29/56
OUTPUT:
Base Nonvirtual called.
Base virtual called.
Base Nonvirtual called.
Derived virtual called.
8/13/2019 c Practical Programs
30/56
COMMAND LINE ARGUMENTS TO PRINT THE GIVENNUMBER IN DESCENDING AND ASCENDING ORDER:
#include
#include
const int MAXSIZE = 10;
void selectionSort(int arr[], int size);
void swap(int& x, int& y);
int main()
{
int numbers[] = { 13, 5, 1, 7, 9, 11, 3, 17, 19, 15 };
int k;
cout
8/13/2019 c Practical Programs
31/56
}
void selectionSort(int arr[], int size)
{
int indexOfMin, pass, j;
for (pass = 0; pass < size - 1; pass++)
{
indexOfMin = pass;
for (j = pass + 1; j < size; j++)
if (arr[j] < arr[indexOfMin])
indexOfMin = j;
swap(arr[pass], arr[indexOfMin]);
}
}
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
8/13/2019 c Practical Programs
32/56
OUTPUT:
BEFORE SORT: 13 5 1 7 9 11 3 17 19 15
AFTER SORT: 1 3 5 7 9 11 13 15 17 19
8/13/2019 c Practical Programs
33/56
CLASSES AND OBJECTS:
#include
#include
class Box
{
public:
double length;
double breath;
double height;
};
int main()
{
clrscr();
Box Box1;
Box Box2;
double volume=0.0;
Box1.height=5.0;
Box1.length=6.0;
Box1.breath=7.0;
Box2.height=10.0;
Box2.length=12.0;
Box2.breath=13.0;
8/13/2019 c Practical Programs
34/56
volume=Box1.height*Box1.length*Box1.breath;
cout
8/13/2019 c Practical Programs
35/56
OUTPUT:
Volume of Box1:210
Volume of Box2:1560
8/13/2019 c Practical Programs
36/56
8/13/2019 c Practical Programs
37/56
return 0;
}
8/13/2019 c Practical Programs
38/56
OUTPUT:
x=20
8/13/2019 c Practical Programs
39/56
8/13/2019 c Practical Programs
40/56
if(a>b)
cout
8/13/2019 c Practical Programs
41/56
8/13/2019 c Practical Programs
42/56
USER DEFINE FUNCTION TO COMPARE TWOSTRINGS:
#include
#include
#include
class kan
{
public:
char str[30],str1[30];
void oper1()
{
coutstr;
coutstr1;
}
void oper2()
{
if(strcmp(str,str1)==0)
{
cout
8/13/2019 c Practical Programs
43/56
else
{
cout
8/13/2019 c Practical Programs
44/56
OUTPUT:
Enter the FIRST string : C++
Enter the SECOND string : C++
Given string both are equal
Enter the FIRST string : C++
Enter the SECOND string : C
Given string both are not equal
8/13/2019 c Practical Programs
45/56
NESTED CLASS:
#include
class mail_info
{
int shipper;
int postage;
public:
void set(int input_class, int input_postage)
{
shipper = input_class;
postage = input_postage;
}
int get_postage(void){ return postage;}
};
class box
{
int length;
int width;
mail_info label;
public:
void set(int l, int w, int ship, int post)
{
8/13/2019 c Practical Programs
46/56
length = l;
width = w;
label.set(ship, post);
}
int get_area(void) { return (length * width);}
};
void main(void)
{
box small, medium, large;
small.set(2,4,1,35);
medium.set(5,6,2,72);
large.set(8,10,4,98);
cout
8/13/2019 c Practical Programs
47/56
OUTPUT:
Area of small box's label is 8
Area of medium box's label is 30
Area of large box's label is 80
8/13/2019 c Practical Programs
48/56
ABSTRACT CLASS:
#include
#include
class A
{
public:
virtual void action() = 0;
};
class B : public A
{
public:
B() {}
void action()
{
cout
8/13/2019 c Practical Programs
49/56
{
cout
8/13/2019 c Practical Programs
50/56
B Binst;
C Cinst;
A.setinst(&Binst);
A.dosomething();
A.setinst(&Cinst);
A.dosomething();
return 0;
getch();
}
8/13/2019 c Practical Programs
51/56
OUTPUT:
Hello world!
Good bye world!
8/13/2019 c Practical Programs
52/56
UNARY AND BINARY OPERATORS:
#include
#include
class Minus
{
private:
int a, b, c ;
public:
Minus(int A, int B, int C)
{
a = A;
b = B;
c = C;
}
void display(void);
void operator - ( );
};
void Minus :: display(void)
{
cout
8/13/2019 c Practical Programs
53/56
}inline void Minus :: operator - ( )
{
a = -a ;
b = -b ;
c = -c ;
}
int main(void)
{
Minus M(5, 10, -15) ;
Clrscr();
cout
8/13/2019 c Practical Programs
54/56
OUTPUT:
Before activating operator - ( )
a= 5
b= 10
c= -15
After activating operator - ( )
a= -5
b= -10
c= 15
8/13/2019 c Practical Programs
55/56
RELATIONAL, LOGICAL AND BITWISE OPERATORS:
#include
#include
int main()
{
unsigned short flags1;
unsigned short flags2;
flags1 = 0x00FF;
flags2 = 0xFF00;
clrscr();
cout
8/13/2019 c Practical Programs
56/56
OUTPUT:
Bitwise Operator: 0
Logical Operator: 1