WAP To Implement Inline Function
WAP To Implement Inline Function
#include<iostream.h>
#include<conio.h>
inline float mul (float x, float y)
{
return (x*y);
}
inline double div (double p, double q)
{
return (p/q);
}
int main ( )
{
clrscr();
float a=12.345;
float b=9.82;
cout<<”multiplication =”<<mul (a,b)<<”\n”;
cout<<division =”<< (a,b)<<”\n”;
getch();
return 0;
}
2
#include<iostream.h>
#include<conio.h>
int m=10;
int main( )
{
clrscr();
int m=20;
{
int k=m;
int m=30;
cout<<”We are inner block\n”;
cout<<”k=”<<k<<”\n”;
cout<<”m=”<<m<<”\n”;
cout<<”::m=”<<::m<<”\n”;
}
cout<<”\n We are in outer block\n”;
cout<<”m=”<<m<<”\n”;
cout<<”::m=”<<::m<<”\n”;
getch();
return 0;
}
3
#include<iostream.h>
#include<conio.h>
class student
public:
student(int,int);
};
student::student(int a,int b)
cout<<”value of A:”<<a<<endl;
cout<<”value of B:”<<b<<endl;
int main()
clrscr();
student stu=student(10,145);
getch();
return 0;
4
#include<iostream.h>
#include<conio.h>
class demo
{
public:
demo ( )
{
cout<<”Hello, I am constructor”<<endl;
}
~demo( ) // Destructor
{
cout<<”Hello, I am destructor”<<endl;
}
};
int main()
{
demo obj1;
return 0;
}
5
#include<iostream.h>
#include<conio.h>
class ABC;
class XYZ
{
int data; //member variable
public:
void set_value(int value)
{
data=value;
}
friend void add(XYZ,ABC); // friend function deceleration
};
class ABC
{
int data;
public:
void set_value(int value)
{
data=value;
}
friend void add(XYZ,ABC);
};
void add(XYZ obj1,ABC obj2) //F.F. definition
{
cout<<"Sum of data values of XYZ and ABC object using
friend="<<obj1.data+obj2.data;
};
6
int main()
{
XYZ X;
ABC A;
X.set_value (5);
A.set_value (50);
add(X,A); //calling friend function
getch ( );
return 0;
}
7
#include<iostream.h>
#include<conio.h>
class test
{
int a,b;
public:
void show( )
{
a=20;
b=15;
cout<<"Object addr="<<this<<endl;
cout<<"\ta="<<this->a<<endl;
cout<<"\tb="<<this->b;
}
};
void main( )
{
clrscr();
test t;
t.show( );
getch();
}
8
#include<iostream.h>
#include<conio.h>
class student
{
int roll;
int marks;
public:
student(int a, int b) // parameter constructor
{
roll=a;
marks=b;
}
student () // Default constructor
{
cout<<"constructor is called when we create an object of student.\n";
}
student(student &t); //copy constructor
void show_data( )
{
cout<<"\nroll:"<<roll<<endl;
cout<<"marks:"<<marks<<endl;
}
};
student::student(student &t)
{
roll=t.roll;
9
marks=t.marks;
}
int main()
{
clrscr();
student jai;
cout<<"Parameter constructor value is:";
student r(40,50);
r.show_data();
cout<<"Copy constructor value is:";
student stu(r);
stu.show_data();
getch();
return 0;
}
10
#include<iostream.h>
#include<conio.h>
class space
{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c);
void display(void);
void operator-(); //overload unary - operator
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout<<"\tx="<<x<<" ";
cout<<"y="<<y<<" ";
cout<<"z="<<z<<"\n";
}
void space::operator-()
{
x=-x;
11
y=-y;
z=-z;
}
int main()
{
clrscr();
space s;
s.getdata(12,-21,31);
cout<<"\ts:";
s.display();
-s; //activate operator -()
cout<<"\n\t-s:";
s.display();
getch();
return 0;
}
12
ptr->display();
getch();
return 0;
}
WAP to show Virtual Destructor:
#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual ~A()
{
cout<<"\nThis is base class destructor"<<endl;
}
};
class B:public A
{
public:
~B( )
{
cout<<"\nThis is derived class destructor"<<endl;
}
};
int main ( )
{
clrscr();
A*ptr=new B ( );
delete ptr;
getch();
return 0;
16
{
cout<<"b="<<b<<endl;
}
};
class derived:public base1,public base2 //derived from base 1and base 2
{
protected:
int c;
public:
derived( )
{
c=55;
}
void showderived( )
{
cout<<"c="<<c<<endl;
}
};
int main( )
{
clrscr( );
derived obj;
obj.showbase1();
obj.showbase2();
obj.showderived();
getch();
return 0;
}
18
{
cout<<"This is customer";
}
};
int main()
{
clrscr ();
manager x;
accountant y;
customer z;
x.getname();
y.getname();
z.getname();
getch();
return 0;
}
21
void Example::print_val()
{
cout<<"val: "<<val<<endl;
}
int main()
{
clrscr();
//create object
Example Ex;
Ex.init_val(100);
Ex.print_val();
22
getch();
return 0;
}
WAP to implement constant member function:
#include<iostream.h>
#include<conio.h>
class Test
{
int value;
public:
Test(int v = 0) {value =v;}
int main()
{
clrscr();
Test t(55);
cout<<"\t"<<t.getValue();
getch();
return 0;
}