0% found this document useful (0 votes)
108 views

WAP To Implement Inline Function

1. The documents provide code snippets to demonstrate various C++ concepts like inline functions, scope resolution operator, constructors, destructors, friend functions, this pointer, operator overloading, inheritance, polymorphism, virtual functions, abstract classes and access specifiers. 2. The code snippets include class definitions with member functions to demonstrate concepts like constructors, destructors, friend functions etc. Main functions are also included to test the class implementations. 3. Various OOPs concepts like inheritance, polymorphism, operator overloading, virtual functions, abstract classes etc. are illustrated through these code snippets.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

WAP To Implement Inline Function

1. The documents provide code snippets to demonstrate various C++ concepts like inline functions, scope resolution operator, constructors, destructors, friend functions, this pointer, operator overloading, inheritance, polymorphism, virtual functions, abstract classes and access specifiers. 2. The code snippets include class definitions with member functions to demonstrate concepts like constructors, destructors, friend functions etc. Main functions are also included to test the class implementations. 3. Various OOPs concepts like inheritance, polymorphism, operator overloading, virtual functions, abstract classes etc. are illustrated through these code snippets.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

1

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

WAP to implement scope resolution operator:

#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

WAP to implement Constructor:

#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

WAP to implement Destructor:

#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

WAP to implement Friend Function:

#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

WAP to implement this pointer:

#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

WAP to show Default, Parameter and Copy


constructor:

#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

WAP to implement Overloading Unary Operator:

#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

WAP to implement Overloading Binary Operator:


#include<iostream.h>
#include<conio.h>
class multiply
{
int first,second;
public:
void getdata(int a,int b)
{
first=a;
second=b;
}
void display()
{
cout<<"first="<<first<<"\nsecond="<<second<<endl;
}
multiply operator *(multiply c); //deceleration
};
multiply multiply::operator *(multiply c)
{
multiply temp;
temp.first=first *c.first;
temp.second=second*c.second;
return temp;
}
int main()
{
clrscr();
13

multiply obj1,obj2,obj3; //Object created


obj1.getdata(15,20); //value passed
obj2.getdata(3,35);
obj3=obj1 * obj2;
obj3.display();
getch();
return 0;
}
14

WAP to show Virtual Function:


#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void display( )
{
cout<<”Hi, this is base class display function”<<endl;
}
};
class derived: public base
{
public:
void display( )
{
cout<<”Hi, this is derived class display function”<<endl;
}
};
int main()
{
clrscr();
base* ptr;
base objB;
derived objD;
ptr=&objB;
ptr->display();
ptr=&objD;
15

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

WAP to show Multiple Inheritance:


#include<iostream.h>
#include<conio.h>
class base1 //base class 1
{
protected :
int a;
public:
base1( )
{
a=10;
}
void showbase1( )
{
cout<<"a="<<a<<endl;
}
};
class base2 //base class 2
{
protected :
int b;
public:
base2( )
{
b=33;
}
void showbase2( )
17

{
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

WAP for overloading base class member in derived class:


#include<iostream.h>
#include<conio.h>
class person
{
public:
void introduce()
{
cout<<"Hi, I am a person"<<endl;
}
};
class student:public person
{
public:
void introduce( )
{
cout<<"Hi, I am a student"<<endl;
}
};
int main( )
{
clrscr();
student a;
a. introduce( );
a.person:: introduce ( );
getch ( );
return 0;
}
19

WAP to implement Abstract class:


#include<iostream.h>
#include<conio.h>
class database
{
public:
virtual void getname()=0;
};
class manager: public database
{
public:
void getname()
{
cout<<"This is manager\n";
}
};
class accountant:public database
{
public:
void getname()
{
cout<<"This is accountant\n";
}
};
class customer:public database
{
public:
void getname()
20

{
cout<<"This is customer";
}
};
int main()
{
clrscr ();
manager x;
accountant y;
customer z;
x.getname();
y.getname();
z.getname();
getch();
return 0;
}
21

WAP to implement Assess Specifier:


#include <iostream.h>
#include<conio.h>
class Example
{
private:
int val;
public:
//function declarations
void init_val(int v);
void print_val();
};
//function definitions
void Example::init_val(int v)
{
val=v;
}

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

// We get compiler error if we add a line like "value = 100;"


// in this function.
int getValue()const {return value;}
};

int main()
{
clrscr();
Test t(55);
cout<<"\t"<<t.getValue();
getch();
return 0;
}

You might also like