Вы находитесь на странице: 1из 28

void main()

{
int n1,n2;
float m,n;
clrscr();
over obj;
cout<<endl<<"enter any one int number:-";
cin>>n1;
cout<<"Enter two int number:-";
cin>>n1>>n2;
cout<<endl<<"enter two float number:-";
cin>>m>>n;
clrscr();
obj.display();
obj.display( n1);
obj.display(m,n);
obj.display(n1,n2);
obj.display();
getch();
}EXAMPLE:-2:-A human being is a perfect example of an object as it consists member data
(parts)like heart,lever,brain etc perfectly encapsulated and member function like
eye,ears,hand,legs etc are provided to access the object and it serve for long time.
Example:-3:-A television is also an example of object as it consists of member data(parts)picture
tube,electrinic circuit enclosed/encapsulated inside cabinet and member function like on/off
switch volume control ,remote are the member function use by end user to access the object.
=>About object oriented features:-
An object or object oriented language must hold least four features:-
1.ENCAPSULATION:-Encapsulation is a method /technique in which member data is
binds/associate with its corresponding function /member function and member data perfectly
encapsulated and member function made publicly open to ensure that the end user access the
object through define member function only.
Example:-A car which is an example of an object go through the mechanism in encapsulation at
the time of manufecturing object in which the automobile engineer binds together member
data(parts) and member function(function) to its corresponding parts like gear box to gear.
2.ABSTRACTION/DATA ABSTRACTION:-Abstraction or data abstraction is the outcome of
encapsulation is an object that can be referred to as data abstraction.
When an object is properly encaplulated i.e member data perfectly hidden and member
function associate to its member data. So that the end user can only access the object through
define member function only and there is no way left to temper the parts then we can the object
is an example of data abstraction.
Example:-A television when view by an user,we found all the parts (member data)like picture
tube ,digital circuit etc are isolated inside the plastic jacket /cabinet properly and member
function function like on/off switch ,menu button etc are open on television.
3.POLYMORPHISM:-
long long
moderate
moderate short

short

Ex:-Fan and regulator may be the example of polymorphism


Ex:-ON/OFF switch is the example of polymorphism
All object Must
follows

“Similar”purpose serving
function must contains
single interface. supports

and consist at
least one
polymorphic
functionFigure shows how an object holds features of
polymorphism
An object oriented language strictly follows the rules stated below similar purpose serving
function must contain similar interface.
When an object follows the rules stated above it must consists at least one polymorphic
function having single interface and producing multiple output.An existance of polymorphic
function having single interface and producing multiple output.An existance of polymorphic
function in an object provides supports features of polymorphism ,which is made of
poly+mor+phism which means one interface and multiple output.Once and object holds features
of polymorphism ,it saves the user complexity.
EX:-A human being which is an example of object consists single interface that is eye using for
multiple purpose like viewing object placed at larger ,moderate and short distance referred to as
polymorphic function
4.INHERITANCE:-
All object and object oriented language must hold the concept of reusability which means code
once define is reusable.To support reusuable feature all object and object oriented language
supports one technique/mechanism that allow inherits of existing class /type in a new class/type
and made things reusable to save the time /resource and such a technique in object oriented
language is referred to as inheritance.
The class/type that inherited is called base class and the
class /type that does inheriting is called derived class in c++.
The derived class /type inherit all the property of its base
class and may contain its own characteristics.
width Furniture[base class]
height
color legs
shelf
arms chair[derived class]
book-
self[derived class]
Fig shows the inheritance:-
Father Base Class

Child Derived Class

Single Inheritance

Father Mother

Base Class Base Class

Child
Derived class

Multiple Inheritance
Grand
Father Base Class

Father Derived Class/Base Class


Grand Son Derived Class

MULTI-LEVEL INHERITANCETypes of Inheritance supported


in c++
The C++ compiler provides a facility to supports three basics types of inheritance as follows:-
1.Single Inheritance:-When a derived Class inherits a single base class is referred to as single
inheritance.
Eg:-An example of single inheritance is a child inherits the characteristics of its father and may
contain its own characteristics.
2.Multiple Inheritance:-(Java will not supports)A derived class inherits more than one base
class ,the inheritance is referred to as multiple inheritance.
Ex:-An example of multiple inheritance is a child inherits the characteristics of its father and
mother and may contain its own characteristics.
3.Multilevel Inheritance:-When a derived class become the base class for another derived class is
referred to as multilevel inheritance.
Ex:-An example of multi-level inheritance is that a grand son inherits the characteristics of its
grand father and father and may contain its own characteristics.
ABOUT OBJECT ORIENTED PROGRAMMING METHODOLOGY In object oriented language works are
accomplished through object and the complete process to accomplish the task through an object
as follows:-
Step:-1:-At first declare object type/class using keyword Class or struct in C++ followed by a
meaninful clas/type name that indicates its purpose and must consists member data properly
encapsulated to store data or information and member function define inside class/type which
publicly open to be used by end user to access the object. Important Notes:-The C++ compiler
provides three keywords/reserve word named public, protected,and private precede with the
member data and member function which ensure its accessibility/visibility referred to as access
specifier in C++ and in java. Access specifier within class
outside class private yes No
public Yes Yes
protected use in case of inheritance1.Private:-A member precede with
access specifier private are accessible within same class only.
2.Public:-A member precede with access specifier public are accessible inside class or outside
class.
3.Protected:-protected access specifier is supported in C++ to deal the case of inheritance.
=>Difference between keyword struct and class.
The keyword class default consider private and the keyword struct consider default public access
specifier.
Syntax to define object type using keyword Class
Class <Class_name> { //scope begin
<access specifier>:
data_type mem_data1,mem_data2,...; //member data
:
:
:
<access specifier>:
<return_type><fun_name>(data_type p1,...)
{
<sequence of statement>
:
:
return(value/var/exp); //optional
}
}obj; //optional to create object
Syntax to define object type using keyword Struct
struct <Class_name> { //scope begin
<access specifier>:
data_type mem_data1,mem_data2,...; //member data
:
:
:
<access specifier>:
<return_type><fun_name>(data_type p1,...)
{
<sequence of statement>
:
:
return(value/var/exp); //optional
}
}obj; //optional to create object
Step 2:-At this moment no object is physically exists and it is only an ideal design/ type
framework. After designing the framework urgent required to allocate/create object and access
the object.
The C++ compiler allowed programmer to create/allocate object of define type as
well as compile time or run time using NEW operator ,which is unlike by java where object
always allocate at run time.
Syntax to create object:-
1.<class type >obj1,..;
2Create object at run time
<class type >*pnaem;
pname=new<class name>;
or
<class name>*pname=new<class name>;
STEP:-3
Once object is physically allocated ,it is ready to use /access object by calling function enclosed
inside object using (.)dot operator or using arrow(->)operator at run time in the following format.
A.When object is used to access using dot operator.The syntax is given below
syn:-object name .member;
B.When object reference /address is used to access member inside the object arrow operator is
used in the following format
Syn:-Pointer->member;
POP OOPS
#include<stdio.h> #include<iostream.h>
#include<conio.h>
#include<conio.h>
void sum(int a,int b) class addition
{ {
int add; int x,y,sum;
add=a+b; public:
printf("The sum of %d and %d is %d",a,b,add); void input()
} {
void main() cout<<"Enter first number:-";
{ cin>>x;
clrscr(); cout<<endl<<“enter second number:-”;
int x,y; cin>>y;
printf("Enter the first number:-"); }
scanf("%d",&x); void calc()
printf("\nEnter the second number:-"); {
scanf("%d",&y); sum=x+y;
sum(x,y); }
getch(); void output()
} {
cout<<“sum=”<<sum;
}//close of function
}//close of class
addition x;
void main()
{
clrscr();
x.input();
x.calc();
x.output();

getch();
}
CHAPTER:- 2
TOPICS:-
->ABOUT CLASS AND OBJECT
->DEFINING CLASS/TYPE IN C++ USING KEYWORD STRUCT/CLASS
->ALLOCATE /INSTATIATE OBJECT IN C++
->DIFFERENT WAY TO ACCESS OBJECT
->DIFFERENT BETWEEN KEYWORD CLASS AND STRUCT
->USE OF SCOPE RESOLUTION(::) OPERATOR IN C++
->USE IF THIS POINTER IN C++
->ABOUT FUNCTION OVERLOADING IN C++
->ABOUT CONSTRUCTOR AND ITS NEED IN C++
->TYPES OF CONSTRUCTOR SUPPORTED IN C++
->ABOUT DESTRUCTOR AND ITS PURPOSE IN C++
->ABOUT STATIC MEMBER AND STATIC CLASS
->ABOUT FRIEND FUNCTION AND FRIEND CLASS
->MISCELLANEOUS
CLASSES AND OBJECT:-In object oriented programming all the task is accomplish through an
object and which requires the following steps:-
Step:-1:-At first define object type/class using keyword struct /class provided by cpp compiler in
the following format which consists member data to store information of object and member
function define inside class to access or perform operation collectively referred to as member of
the class and must be encapsulated using keyword private,public and protected which referred to
as access specifier in C++.
#include<iostream.h>
#include<conio.h>
class employee{
char name[25];
long int ecode;
float salary;
public:
void input(){
cout<<"Enter the employee name:-";
cin>>name;
cout<<endl<<"Enter the employee code:-";
cin>>ecode;
cout<<endl<<"Enter the salary:-";
cin>>salary;}
void display(){
clrscr();
cout<<"=======Employee Details=======";
cout<<endl<<endl<<"Name:-\t\t"<<name;
cout<<endl<<"Emp_code:-\t"<<ecode;
cout<<endl<<"Emp_salary:-\t"<<salary;
cout<<endl<<"===============================";} };
employee x;
void main(){
x.input();
x.display();
getch();}Using struct
#include<iostream.h>
#include<conio.h>
class employee{
char name[25];
long int ecode;
float salary;
public:
void input({
cout<<"Enter the employee name:-";
cin>>name;
cout<<endl<<"Enter the employee code:-";
cin>>ecode;
cout<<endl<<"Enter the salary:-";
cin>>salary;}
void display()
{ clrscr();
cout<<"=======Employee Details=======";
cout<<endl<<endl<<"Name:-\t\t"<<name;
cout<<endl<<"Emp_code:-\t"<<ecode;
cout<<endl<<"Emp_salary:-\t"<<salary;
cout<<endl<<"===============================";
}
};
employee x;
void main()
{
clrscr();
x.input();
x.display();
getch();
}
using pointer
#include<iostream.h>
#include<conio.h>
struct employee
{ private:
char name[25];
long int ecode;
float salary;
public:
void input()
{
cout<<"Enter the employee name:-";
cin>>name;
cout<<endl<<"Enter the employee code:-";
cin>>ecode;
cout<<endl<<"Enter the salary:-";
cin>>salary;
}
void display()
{ clrscr();
cout<<"=======Employee Details=======";
cout<<endl<<endl<<"Name:-\t\t"<<name;
cout<<endl<<"Emp_code:-\t"<<ecode;
cout<<endl<<"Emp_salary:-\t"<<salary;
cout<<endl<<"===============================";

}
};
employee x; //global object

void main()
{ employee x; //local object
employee *p;
p=&x;
clrscr();
p->input();
p->display();
getch();
}B:-The second step is to allocate object of define object type/class and this can be
accomplish at compile time as well as run time using the syntax given below
1.compile time ->local object ->global object
2.Run time
1.compile time:-we can allocate object at compile time either in 4th region i.e stack referred to as
local object which holds the same property of stack and 2nd region i.e global region which holds
the same property of global region.
Syntax to allocate local object:-
{
<class name> object1....;
}
Syntax to allocate global object
class <class name>
{
<access specifier>:
mem_data;
mem_data;
<access specifier>:
<mem_fun>
}obj; //global object
class name obj; //global object
2.Run time:-We can allocate object at run_time and the C++ compiler provide two operators
called new and delete to allocate object in third region which is also known as heap region.
Syntax to allocate object in heap region
<class name>*ptrname;
ptrname=new<class name>;
or
<classname>*ptrname=new<classname>;
Syntax to deallocate object:-
delete ptrname;
delete[] ptrname; //array of object
Note:-Must explicitly deallocate object using delete operator after completion of task.
step:-3:-When object is finally exists it is ready to access or use.
Different way to access the object:-
=>using dot(.) operator =>Using arrow(->) operator
=>When a object name is used to access member inside object dot operator is used in the
following format
Syn:-objname.member;
=>When object reference is used to access member inside/within object arrow operator is used in
the following format:-
Syn:-ptr_name->member;

#include<iostream.h>
#include<conio.h>
struct employee{
private:
char name[25];
long int ecode;
float salary;
public:
void input(){
cout<<"Enter the name of employee:-";
cin>>name;
cout<<endl<<"Enter the employee code:-";
cin>>ecode;
cout<<endl<<"Enter the salary of Employee:-";
cin>>salary;}
void output(){
cout<<endl<<"The name of employee:-\t\t\t"<<name;
cout<<endl<<"The code of employee is:-\t\t"<<ecode;
cout<<endl<<"The salary of employee is:-\t\t"<<salary;
}//close of function
};//close of class
employee x;//global object
void main(){
clrscr();
x.input();
clrscr();
x.output();
getch();
}

#include<iostream.h>
#include<conio.h>
struct employee{
private:
char name[25];
long int ecode;
float salary;
public:
void input(){
cout<<"Enter the name of employee:-";
cin>>name;
cout<<endl<<"Enter the employee code:-";
cin>>ecode;
cout<<endl<<"Enter the salary of Employee:-";
cin>>salary;}
void output(){
cout<<endl<<"The name of employee:-\t\t\t"<<name;
cout<<endl<<"The code of employee is:-\t\t"<<ecode;
cout<<endl<<"The salary of employee is:-\t\t"<<salary;
}//close of function
};//close of class

void main(){
employee x;
x.input();
clrscr();
x.output();
getch();
}
#include<iostream.h>
#include<conio.h>
struct employee{
private:
char name[25];
long int ecode;
float salary;
public:
void input(){
cout<<"Enter the name of employee:-";
cin>>name;
cout<<endl<<"Enter the employee code:-";
cin>>ecode;
cout<<endl<<"Enter the salary of Employee:-";
cin>>salary;}
void output(){
cout<<endl<<"The name of employee:-\t\t\t"<<name;
cout<<endl<<"The code of employee is:-\t\t"<<ecode;
cout<<endl<<"The salary of employee is:-\t\t"<<salary;
}//close of function
};//close of class

void main(){
employee x,*px;
px=&x;
px->input();
clrscr();
px->output();
getch();
}

#include<iostream.h>
#include<conio.h>
struct employee{
private:
char name[25];
long int ecode;
float salary;
public:
void input(){
cout<<"Enter the name of employee:-";
cin>>name;
cout<<endl<<"Enter the employee code:-";
cin>>ecode;
cout<<endl<<"Enter the salary of Employee:-";
cin>>salary;}
void output(){
cout<<endl<<"The name of employee:-\t\t\t"<<name;
cout<<endl<<"The code of employee is:-\t\t"<<ecode;
cout<<endl<<"The salary of employee is:-\t\t"<<salary;
}//close of function
};//close of class

void main(){
employee x,*px;
px=&x;
px->input();
clrscr();
px->output();
getch();
}
//program to display today,s date
#include<iostream.h>
#include<conio.h>
class time{
int dd,mm,yy;
public:
void gettime(int d,int m,int y){
dd=d; mm=m; yy=y;}
void display(void){
cout<<endl<<"Today's date is:-";
cout<<dd<<"/"<<mm<<"/"<<yy;} };
void main(){
clrscr();
time t,*pt;
pt=&t;
pt-> gettime(22,12,2011);
pt->display();
getch();}#include<iostream.h>
#include<conio.h>
class complex{
float real,image;
public:
void accept(float r,float i)
{
real=r;
image=i;
}
void display(){
if(image>=0)
cout<<real<<"+"<<image<<"i";
else
cout<<real<<image<<"i";
}
}x;//object
void main()
{
clrscr();
x.accept(4,3);
x.display();
getch();
}

Ques:-Write an abstract data type named time which consists two members :-
1.Member data to store time in hour,minute and second (int)
2.Member function (A):-void readtime(int,int int)to accept the time
(B):-void printf() to display time in hh:min:sec
#include<iostream.h>
#include<conio.h>
class time{
int hour,min,sec;
public:
void readtime(int h,int m,int s){
hour=h;
min=m;
sec=s;
if(sec>59)
{
min=min+sec/60;
sec=sec%60;
}
if(min>59)
{
hour=hour+min/60;
min=min%60;
}//close of if
}//close of functionvoid print(){
cout<<endl<<hour<<":"<<min<<":"<<sec;
}
};//close of class
void main(){
time t;
t.readtime(20,89,61);
t.print();
getch();
}
Ques:-#include<iostream.h>
#include<conio.h>
struct sample{
private:
int x,y;
public:
void accept(){
cout<<"Enter the first number:-";
cin>>x;
cout<<endl<<"Enter the second number:-";
cin>>y;}
void sum(){
cout<<endl<<"The sum of nos is"<<x+y;}
void sub(){
cout<<endl<<"The subtraction of"<<x-y;}
void mul(){
cout<<endl<<"The multiply of "<<x*y; }
void div(){
cout<<endl<<"The division of " <<x/y;}
}; //close of class
void main()
{ sample x,*px;
px=&x;
clrscr();
px-> accept();
px-> sum();
px-> sub();
px-> mul();
px-> div();
getch(); }

What is the use of scope resolution operator in C++:-The C++ compiler allowed programmer to
define member function body outside class (i.e function definition)using scope resolution
operator(::) .In order to define member function definition outside class it is required include its
prototype inside class to compile without error.
Syntax to declare prototype inside class:-
class<classname>
{
<access specifier>:
<return type>member function();
<return type>member function(datatype,...);
<return type>member function(datatype&,,,,);
}object; //create object optional
syntax to define member function outside class
<return type><class name>::member function(data type para1...){
sequence of statement;
:
:
return(value/var/exp);
#include<iostream.h>
#include<conio.h>
class add
{
int month,day,year;
public:
void input(int,int,int);

void output();

}x,*p;
void add::input(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
void add::output()
{
cout<<"Today's date is ";
cout<<day<<"/"<<month<<"/"<<year;
}

void main()
{
p=&x;
p->input(3,1,2012);
p->output();
getch();
}#include<iostream.h>
#include<conio.h>
class sample
{
int x,y;
public:
void accept();
void sum();
void sub();
void multiply();
void div();
void mod();
};
void sample::accept()
{
cout<<"Enter the first number:-";
cin>>x;
cout<<endl<<"Enter the second number:-";
cin>>y;
}
void sample:: sum()
{
cout<<endl<<"sum of nos="<<x+y;
}
void sample:: sub()
{
cout<<endl<<"Sub of nos="<<x-y;
}
void sample:: multiply()
{
cout<<endl<<"Multiply of nos="<<x*y;
}
void sample:: div()
{
cout<<endl<<"Division of nos ="<<x/y;
}
void sample:: mod()
{
cout<<endl<<"Mode of nos="<<x%y;
}
void main()
{
sample a;
a.accept();
a.sum();
a.sub();
a.multiply();
a.div();
a.mod();
getch();
}
Ques:-Write an ADT named complex which consists following members :-
1.Define member data real(float) and imaginary(float) that can store the real and imaginary part
of complex number.
2.member function:-
A.void accept(float,float):-member function to accept complex number and store it in
corresponding member data.
B.Void display():-Member function to print the complex number in given format
real + imaginary
-
7+3i
0+0i
5-2i
#include<iostream.h>
#include<conio.h>
struct complex
{
float real,imag;
public:
void accept();
void display();
};
void complex::accept()
{
cout<<"Enter the real part:";
cin>>real;
cout<<endl<<"Enter the imaginary part:-";
cin>>imag;
}
void complex:: display()
{
if(imag>=0)
cout<<real<<"+"<<imag<<"i";
else
cout<<real<<imag<<"i";
}
void main()
{

complex x;
x.accept();
clrscr();
x.display();
getch();
}
#include<iostream.h>
#include<conio.h>
class student
{
long int enroll;
float phy,che,math,total;
public:
void accept();
void cal_result();
void display();
};
void student::accept(){
cout<<"Enter the enrollment number of student-";
cin>>enroll;
cout<<endl<<"Enter the marks of physics:-";
cin>>phy;
cout<<endl<<"Enter the marks of chemistry:-";
cin>>che;
cout<<endl<<"Enter the marks of Mathematics:-";
cin>>math;
}
void student::cal_result()
{
total=phy+che+math;

}
void student::display()
{
cout<<endl<<"----------STUDENT INFORMATION-------------";
cout<<endl<<"ENROLLMENT NUMBER="<<enroll;
cout<<endl<<"MARKS OF PHYSICS="<<phy;
cout<<endl<<"MARKS OF CHEMISTRY="<<che;
cout<<endl<<"MARKS OF MATHEMATICS="<<math;
cout<<endl<<"------------------------------------------";
cout<<endl<<endl<<endl<<"TOTAL MARKS OBTAIN BY STUDENT="<<total;
}
void main()
{
student x,*px;
px=&x;
px->accept();
px->cal_result();
clrscr();
px->display();
getch();
}

USE IF “THIS” POINTER IN C++.


DEF:-It is sometimes required by the member function to know the address/refrence of the
current object that invoke/called the member function,the c++ compiler provides the predefine
pointer named “this” which always contain the reference of current and can be used /access
inside the member function.

when formal parameter of a member function defined same name as that of member data you
urgently need the “this” pointer to name space collasion,which is one of the major advantage of
“this” pointer.
Explain the use of “this” pointer with example:-
#include<iostream.h>
#include<conio.h>
class complex
{
int real,image;
public:
void input(int real,int image){
this-> real=real;
this->image=image;
}
void output()
{
if(image>=0)
cout<<endl<<real<<"+"<<image<<"i";
else
cout<<endl<<real<<image<<"i";
}
}c;
void main()
{ int a,b;
cout<<"enter the real part:-";
cin>>a;
cout<<endl<<"enter the imaginary part:-";
cin>>b;
c.input(a,b);
c.output();
getch();
}Write a program in C++ to calculate the factorial of given number using oops
#include<iostream.h>
#include<conio.h>
class factorial{
long int calc;
public:
int fact(int f) {
int i, calc=1;
for(i=1;i<=f;i++)
calc=calc*i;
return calc;}
}abc;
void main(){
long int res;
int num;
cout<<endl<<"Enter any number:";
cin>>num;
res=abc.fact(num);
cout<<"factorial of number:-"<<res;
getch();}//using scope resolution operator
#include<iostream.h>
#include<conio.h>
class calculation
{
long int temp;
public:
int factorial(int num);
}x;
int calculation::factorial(int num)
{
temp=1;
int i;
for(i=1;i<=num;i++)
{
temp=temp*i;
}
return (temp);
}
void main()
{
int n;
long int res;
cout<<"enter any number:-";
cin>>n;
res=x.factorial(n);
cout<<"Factorial="<<res;
getch();
}FUNCTION OVERLOADING:-The C++ compiler allow a programmer to define any number
of function/member function having same name.In function overloading,the number of
parameter can be differ,the data type of the parameter can be differ or the sequence of parameter
can be differ.The above discusssed rule follow the same interface producing different output
reffered to as function overload and the process involved is called function overloading in C++
and may be the example of polymorphism.
Note:-No two function can be overloaded on the basis of return type.
Compiler error:-
flaot sum(int a,int b){
return(a+b);
}
int sum(int a,int b){
return(a+b);
}

#include<iostream.h>
#include<conio.h>
float sum(float a,float b){
return a+b;
}
float sum(float a,float b,float c)
{
return a+b+c;
}
void main(){
clrscr();
cout<<endl<<"Sum of two nos="<<sum(10,30);
cout<<endl<<"sum of two nos="<<sum(11.11,22.22,33.33);
getch();
}#include<iostream.h>
#include<conio.h>
#include<dos.h>
void swap_int(int a,int b){
int temp;
temp=a;
a=b;
b=temp;
cout<<endl<<"Number after swapping";
cout<<"A="<<a<<"B="<<b;}
void swap_float(float a,float b){
float temp;
temp=a;
a=b;
b=temp;
cout<<endl<<"Number after float swap";
cout<<"A="<<a<<"B="<<b;}
void swap_char(char a,char b){
char temp;
temp=a;
a=b;
b=temp;
cout<<endl<<"character after swap";
cout<<"A="<<a<<"B="<<b;}
void main()
{
int choice,x,y;
float m,n;
char c,d;
clrscr();
while(choice){
cout<<endl<<"==============Menu Selection==============";
cout<<endl<<"Press 1 for integer swap";
cout<<endl<<"Press 2 for float swap";
cout<<endl<<"Press 3 for char swap";
cout<<endl<<"press 4 for quit application";
cout<<endl<<"===================================";
cout<<endl<<"Enter your choice:-";
cin>>choice;
switch(choice){
case 1:
cout<<endl<<"Enter first number:-";
cin>>x;
cout<<endl<<"Enter second number:-";
cin>>y;
swap_int (x,y);
break;

case 2:
cout<<endl<<"Enter first number:-";
cin>>m;
cout<<endl<<"Enter second number:-";
cin>>n;
swap_float(m,n);
break;
case 3:
cout<<endl<<"Enter first character:-";
cin>>c;
cout<<endl<<"Enter second character:-";
cin>>d;
swap_char(c,d);
break;
default:
cout<<endl<<"Invalid menu selection,Try again:";
delay(3000);
return;
}//close of switch
}//close of while
getch();}

#include<iostream.h>
#include<conio.h>
#include<dos.h>
void swap(int a,int b){
int temp;
temp=a;
a=b;
b=temp;
cout<<endl<<"Number after swapping";
cout<<"A="<<a<<"B="<<b;}
void swap(float a,float b){
float temp;
temp=a;
a=b;
b=temp;
cout<<endl<<"Number after float swap";
cout<<"A="<<a<<"B="<<b;}
void swap(char a,char b){
char temp;
temp=a;
a=b;
b=temp;
cout<<endl<<"character after swap";
cout<<"A="<<a<<"B="<<b;}
void main()
{
int choice,x,y;
float m,n;
char c,d;
clrscr();
while(choice){
cout<<endl<<"==============Menu Selection==============";
cout<<endl<<"Press 1 for integer swap";
cout<<endl<<"Press 2 for float swap";
cout<<endl<<"Press 3 for char swap";
cout<<endl<<"press 4 for quit application";
cout<<endl<<"===================================";
cout<<endl<<"Enter your choice:-";
cin>>choice;
switch(choice){
case 1:
cout<<endl<<"Enter first number:-";
cin>>x;
cout<<endl<<"Enter second number:-";
cin>>y;
swap (x,y);
break; case 2:
cout<<endl<<"Enter first number:-";
cin>>m;
cout<<endl<<"Enter second number:-";
cin>>n;
swap(m,n);
break;
case 3:
cout<<endl<<"Enter first character:-";
cin>>c;
cout<<endl<<"Enter second character:-";
cin>>d;
swap(c,d);
break;
default:
cout<<endl<<"Invalid menu selection,Try again:";
delay(3000);
return;
}//close of switch
}//close of while
getch();}
Use of function overload
#include<iostream.h>
#include<conio.h>
class over
{
int x,y;
float a,b;
public:
void display(){
cout<<endl<<"*************************";
}
void display(int a)
{ x=a;
cout<<endl<<"Value of single argument:-"<<x;

}
void display(float y,float z)
{
a=y;
b=z;
cout<<endl<<"Sum of two parameter is:-"<<a+b;
}
void display(int a,int b){
this->x=a;
this->y=b;
cout<<endl<<"Addition of two nos="<<x+y;
}
}; //close of class void main()
{
int n1,n2;
float m,n;
clrscr();
over obj;
cout<<endl<<"enter any one int number:-";
cin>>n1;
cout<<"Enter two int number:-";
cin>>n1>>n2;
cout<<endl<<"enter two float number:-";
cin>>m>>n;
clrscr();
obj.display();
obj.display( n1);
obj.display(m,n);
obj.display(n1,n2);
obj.display();
getch();
}BIT BY BIT COPY TECHNIQUE:- The C++ compiler allow that when two similar
object assign to each other,the member data of an object on right side of an assignment operator
is automatically copy bit by bit on the corresponding member data of an object on the left of
assignment operator and the technique is known as bit by bit technique.
Example to show the bit bit technique
#include<iostream.h>
#include<conio.h>
class complex{
float real,image;
public:
void input(){
cout<<endl<<"Enter the real part:-";
cin>>real;
cout<<endl<<"Enter the imaginary part:-";
cin>>image; }
void display(){
if(image<0)
cout<<endl<<real<<image<<"i";
else
cout<<endl<<real<<"+"<<image<<"i";}
}x,y;
void main(){
clrscr();
x.input();
x.display();
y=x;
y.display();
getch();
}TOPICS:-PASSING OBJECT AS AN ARGUMENT TO CALLED MEMBER
FUNCTIONQues:-How object can be passed to called member function in c++.Describe with
example.
Ques:-Explain the concept of call by value and call by reference technique by giving example of
each one.
ANS:-In C++ an object can be passed as value as well as reference to called member function ,
referred to as an example of call by value and call by reference technique.
A.Call by Value:-When an object is passed as an argument to called member function and bit by
bit copy technique takes place in which the member data of an object passed as an argument
,copy on the corresponding member function of the parameterized object,is referred to as an
example of call by value.
<return_type><class name>::mem_fun(<class_name>obj .par1,,,,)
{
sequence of statement;

return(value/exp/ref);
}
Syn to call member function by passing object as an argument.
[var/obj=]mem_fun(obj1,,,); //call by value

Example to demonstrate a case of call by value


#include<iostream.h>
#include<conio.h>
class comple{
float real,image;
public:
void input(){
cout<<endl<<"Enter the real number:-";
cin>>real;
cout<<"Enter imaginary part:-";
cin>>image;}
void display(){
if(image>=0){
cout<<endl<<real<<"+"<<image<<"i"; }
else
cout<<endl<<real<<image<<"i";}
void calc(complex s,complex t){
real=s.real+t.real;
image=s.image+t.image;}
};//close of class
void main(){
clrscr();
complex x,y,z;
x.input();
x.display();
y.input();
y.display();
z.calc(x,y);
z.display();
getch();
}

Вам также может понравиться