Nothing Special   »   [go: up one dir, main page]

C Language

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 65

C++ Language

Introduction
C++ Tokens
Data Types
Operatores
Control statements
Arrays
Pointers
Memory management operator
Functions: Inline function, Reference variable,
Function Overloading.
C++ Language
 Structure
 Union
 Enumaration: enum
 Class
 Data Hiding
 Abstraction
 Polymorphism
 Inheritance
Introduction
 It is an expanded version of C – language.
 Its Developed at Bell lab USA by Bjarne
Stroustrup in 1980. Manage large Programs.
 C++ -> C with OOPS Concept
 Object Oriented programming was developed
to reduce the inherent limitations of traditional
Programming Languages.
Introduction Cont…..
 It follows the bottom up designing approach.
 C++ programs are easily maintainable and
expendable.
 C++ Supports reusability.
 It also supports the complexity of program.
 We can make object oriented libraries which
can be used later by many program.
 It helps to solve the real world program.
 Its provide security to the data.
 It is extended from C. Its supports all features
of C.
Introduction Cont…..
 Class : Class is a user defined data types
contained data and code.
 Class can be defined like given below the code
and data line together encapsulated into a
single unit called class.
 Using class you can create user defined data
type.
 Class can be used to create similar type of
objects . It is similar to variable declaration.
 Example : flowers,cars, etc.
Introduction Cont…..
 Object :Object is an instant of class.
 It is a real time entities.
 Based on the class you can create any number
of similar type.
 Ex. Maruti for class car.
Rose for class flower.
Introduction Cont…..
 Data Abstraction : Hiding the internal
complexity of the program is called data
abstraction.
 The powerful way to manage the abstraction is
through the hierarchical classification. In this
breaking the task into more manageable
pieces.
Introduction Cont…..
 Data Encapsulation : Encapsulation is
a mechanism which data and code together into
a single unit.
 Object is an logical entity that encapsulates
both data and code.
 Data hiding : Hiding the internal
implementation of the program called data
hiding that can be achieved by using classes.
Introduction Cont…..
 Inheritance : Inheritance is a process to
create a new class called Derived class from
the existed or Base class.
 It is the process by which one object acquires
the features of the another object.
 It is important because it supports the
hierarchical classification, the knowledge is
more manageable.
 By this we can eliminate the redundant code
and extend the use of existing classes.
Introduction Cont…..
 Polymorphism :OOPs languages
supports Polymorphism which is “One interface,
multiple method”.
 It reduces the complexity of the program by
allowing the same interface for general class of
action.
 Message Passing :Message Passing or
Object Communication : Two objects can be
communicated by exchanging the information.
Introduction Cont…..
 Object Based Programming Languages :
Languages that does not support Inheritance
and Run – time polymorphism.
 Ex. C, FORTRAN , PASCAL and COBOL, etc.

 Object Oriented Programming Languages :


Object based + Inheritance + run – time
polymorphism.
 Ex. C++, Java, etc.
Introduction Cont…..
 Syntax : <Header file>
class declaration;
fucntion definition;
void main()
{
statements;
}
 Comment :C++ provides :
 Single line Comment : (By double slace ‘//’).
 Multi line Comment : ( Encloses between /*
comments */).
C++ Tokens
 C++ providing following tokens :
Identifiers, Variables, Keywords, Operators,
Constants, Strings.
 Keywords :These meaning is there in our
compiler, these are known as a reserve words,
they should be in lower case.
 Ex. : int, float, char, etc.

 Constant :Constant is these value can not


be changes at run time . Constant is divided
into two parts : Numeric, and character
constant.
Tokens Cont…..

 Constants is following types :


 Numeric constant : it is two types (1) Integer
constant : e.g., 1,12,0,-98..etc.Range is system
dependent, on some system -32768 to 32767.
(2) Floating point constant.
 Character constants.
Data Types
 C++ is allows mainly three types of data types
: (1)Primitive, (2) Derived, (3) User defined.
 Primitive data types : These are providing
our system/Computer. e.g. int, float, long,
double, unsigned int etc.
 Derived data types :C++ providing three
types of derived data types : Arrays, Function,
Reference variable.
 User defined data types : User can create
own data types from primitive data types. It is
four types : (1)Structure, (2)Union,
(3)Enumeration, (4)Class.
C++ Operators
1. Arthematic : +, -, *, /, % etc.
2. Relational : <, <=, >, >=, ==, !=, etc.
3. Logical : &&, ||, !, etc.
4. Bitwise : &, |, ^, ~, etc.
5. Increment Decrement : ++, - -, etc.
6. Conditional : ?, :, etc.
7. Shift : << (left shift), >> (right shift).etc.
8. Sizeof : sizeof,.
9. Memory management :
10. Scope resolution : :: (two colon).
11. Member : . (dot)
12. Dot star : .* ( or member pointer)
13. Arrow star : ->*
C++ Skip sequence
 \n : new line
 \a : a sound from cpu.
 \t : for a tab space.
 \b : for go to one character space back.
 \f : for horizontal tab.
 \v : for vertical tab.
 \r : go to back given no of character start to
print.
 \’ : for printing ‘ ( single comma).
 \” : for printing “ ( inverted comma).
 \\ : for printing \ (forward slace).
Control statements
 It will be control flow of execution.
 C++ providing two types of control statements:
Conditional, and Looping.
 Conditional control statements : if, if – else .
 Syntax : if(condition)
{----------------------------------------------------------------
---------}
 All syntax is same as C – language.
 Switch(value)
{
case value : statement; break;
Default : statement;
} Note : 1 to 257 cases allow.
Control statements Cont…..

 Ternary operator or conditional operator :


 Syntax :
true false
 (Condition)? Statement 1 : sataement2;
 It will work same like if else statement.
 Example :
 (a>b)?cout<<“a is large” : cout<<“b is large “;
 When a is greater the true statement executes
a is large , if condition is false then statement
executes b is large.
Control statements Cont…..

 Looping Statements :
 While(condition){ ----statements----}
 do {---statements---} while(condition);
 for (initialization; condition; increment / decrement){--stmts--}
Arrays
 Arrays is the collection of elements with similar
type.
 C++ provides two types of arrays: (i) Single
dimension, and (ii) Multi dimension.
 Example : (i) Data_type A_name[size]={Value
list};
int m[5];
Int [m]={10,35,60,75,25};
(ii)Data_type A_name[row_size][column_size];
int m[4][3]; char str[4][5]; int[2][3][4];
int[3][3]={12,1,2,4,34,2,5,45,3};
Pointers
 Pointer is a variable i.e. allows address of some
other variable. e.g. int a=10;
int *p; p=&a;
(i)Const : const int i=20;
(ii)Constant pointer : int a=10; int *const p=&a;
//here address is constant not data.
*p=*p+2;
Cout<<p;
// p=p+3; This is const pointer so we can’t
increase address of pointer.
Cout<<&p;//error illegal structure operation.
Pointer cont…..
 Pointer const :Here its only possible to modify
address of variable.
 Example : int a=10; int const *p=&a; cout<<p; //
*p=*p+20; This is pointer constant
//Cout<<p; Error illegal structure operation.
Note : Here its not possible to modify that data &
address.
Void main()
{ int a=10; const int *const p=&a; Cout<<*p; //
cout<<p; Error illegal structure operation.
}
Memory management operator
 C++ : ‘new’ is used instead of malloc(), calloc(),
realloc(), And ‘delete’ instead of free() in
respect of C-language.
 C uses malloc() and calloc() functions to
allocate memory dynamically at run time.
 Similarly, its uses the function free() to free
dynamically allocated memory.
 C++ supports these functions its is also define
a two unary operators ‘new’ and ‘delete’.
 That perform the task of allocating and freeing
the memory in a better and easier way.
 These are also called free store operator.
Memory management op. cont…..
 New operator : The ‘new’ operator can be used to
create object of any type.
 Syntax : pointer_variable=new data_type;
 Example : int *p; p=new int; or int *p=new int; *p=34;
or int *p=new int(30);
 Delete operator : It is destroyed to release the memory
space for reuse.
 Syntax : delete pointer_variable;
 For array : delete [ ]pointer_variable;
• How to allocate memory for array :
• General form for one dimensional array is :
pointer_variable=new data_type[size];
Memory management op. cont…..
 For multi dimensional array :
array_pointer=new int[3][4][5]; array_ptr=new
int[n][5][4];
 Invalid declaration :
Array_ptr=new int[3][4][ ];
Array_ptr=new int[ ][4][5];
 Releasing the memory at run time :
Syntax : delete [ ]p;
Functions
 Function is a set of statements that are using to solve particular
task.
 C++ is providing two types of function : (i)Library function,
(ii)User defined function
 Library function : These functions are pre-written and pre
compiled functions. E.g. getch();
 Getch(): Input only one character
 Strcpy(dest, source) : Copy String from source to destination.
 Strcmp(): Camparison of two string.
 Strcat(): Concatenat two string.
 Strlen() : Calculate length of String.
 Clrscr() : Clear the text mode window.
 Sqrt(): Calculates the positive square root of the input value.
Functions
 Strupr(): Convert lowercase String to
Uppercase.

 User defined function : User can create his


own function here mainly three steps are
involved. (i) function declaration. Or prototype
declaration (ii) function calling. (iii) function
definition
 Syntax: Ret_type func_name(parameters){ }
Functions cont…..
 Void dis();void main(){ dis();dis();} void dis()
{ cout<<“Welcome to CMC “<<endl;}
 Argument without re_type : void add(int, int);
Void main(){ int a=20,b=30; add(a,b); }
Void add(int x, int y){ cout<<x+y; }
 Arguments with Re_type : int add(int, int);
Void main(){ int a=10,b=20,c;
C=add(a,b); cout<<c; }
Int add(int x, int y)
{ return(x+y);
}
Inline Functions
 Inline function is the small function, it will be working
same like macros.
 Rules of inline function :-
• Function heading must contain ‘inline’ which is a
‘reserve’ word.
• No prototype for inline function.
• It must be a small function.
• It cannot contain control structures such as while loop,
for loop, do-while loop etc.
• In any of these rules are violated then compiler does
not give any error instead the compiler treats that
function as normal function. Thus inline is only a
‘Request’ to compiler but not demand.
• Advantage: program execution become faster.
Comparison between Normal & Inline function
NORMAL FUNCTION INLINE FUNCTION
 When the function is called  When function is called
‘Branching’ take place. ‘substituition’ take place.
 Slow execution since  Faster execution since no
branching is involved. branching.
 It can contain any no. of  It must be a small function.
statements.  No control structure.
 Control structures are
allowed.
Inline function Notes.
cont…..

 Whenever an inline( ) is called substituition takes


place i.e. ‘F1( )’ is replaced by the three ‘cout’
statements.
 The substituition will be performed by compiler during
compilation time.
 Example : inline void F1( ){ cout<<“Hyd\n”;
cout<<“Seen\n”; cout<<“Cyb”; }
void main( )
{ F1( ); F1( ); }
Functions cont... Default Argument :
 C++ allows to call a function without specifying all its
arguments. In such case the function assign a default value to
the parameter which does not have a matching arguments in
the function call.
 Default values are specified when the function is declared. The
compiler look at the prototype to see how many arguments a
function uses and alert the program.
 You must add default arguments from right to left.
 Example : int mul(int j=5; int k=10); // valid
Int mul(int j=5, int k) // invalid
Int mul(int l=10,int j, int k=9) // invalid
Int mul(int l=10,int j=3; int k=9) // valid
 Advantage: Default arguments are useful in situations where
some arguments always have the same value.
 Example : void add(int x, int y=20, int z=20); void main( )
{ add(5,30,40); } void add(int x, int y, int m){ cout<<x+y+z; }
Functions cont…..
 Call by Value :example : void swap(int, int); void main( ){ int
a,b; a=10; b=20; swap(a,b); cout<<“After swap
a=“<<a<<“b=“<<b; }
void swap(int x, int y){ int tmp; tmp=x; x=y; y=tmp; }
 Call by address :example : void swap(int *, int *);
void main(){int a=10, b=20; cout<<“before swap a & b<<a<<”“<<b;
Swap(&a,&b); cout<<“after swap a=“<<a<<“ b=“<<b; }
Void swap(int *x, int *y)
{
int tmp;
tmp=*x;
*x=*y;
*y=tmp;
}
Reference Variable :
 A reference variable provides an alias (alternative name) for
previously defined variable.
 Syntax :data_type &reference_name=variable_name;
Example: float tot=10; float &sum=tot;
 Restriction to reference : A reference variable must be
initialized at the time of declaration.
Example: float &sum; sum=tot; (Invalid)
 We cannot reference another reference.
example: int a=10; int &x=a; int &y=x; (Invalid)
 We can’t create arrays of reference.
Example: int a[10]; int &x=a; (invalid)
#include<iostream.h>
Void main(){ int a=10; int &x=a; cout<<x; }
Reference Variable :
 #include<iostream.h>
Void swap( int &x, int &y);
Void main()
{
int a=10,b=20;
cout<<“Before swap a=“<<a<<“ b=“<<b;
swap(a,b);
cout<<“After swap a=“<<a<<“ b=“<<b<<endl;
}
Void swap(int &x, int &y)
{
int tmp;
tmp=x; x=y; y=tmp;
}
 Note:In above function ‘x’ and ‘y’ alias variable not taking any
byte of memory.
Function Overloading :
 Function overloading or function polymorphism.
 Function overloading is the process of using the same name for
two or more functions.
 Each redefinition of a function must use different type of
parameters, or, different sequence of parameters, or different
number of parameters.
 The number, type or sequence of parameters for a function is
called the function signature.
 Example: void dis(int); void dis(int, int); void dis(int, char);
Void main()
{ int a=10, b=20; char x=‘A’; dis(a); dis(a,b); dis(b,x); }
Void dis(int x){ cout<<x; }
Void dis(int x, int y){ cout<<x<<“ “<<y<<endl; }
Void dis(int x, char y){ cout<<x<<“\t”<<y<<endl; }
Structure
 Structure is a collection of variables and member functions.
 By default structure members are public.
 Syntax: struct tag_name
{
data_type var1,var2,……..;
data_type var1,var2,……..;
Return_type func_name( )
{
statements1;
statements2;……………………;
}
};
Structure cont…...
 Example: (1). struct emp{ int no; char name[20]; int sal; };
Void main()
{ emp e={10,”cmc”,6000}; cout<<e.no<<e.name<<e.sal<<endl; }
Void main()
{ emp e; e.no=90; strcpy(e.name,”Ramarao”); e.sal=9000;
cout<<e.no<<e.name<<e.sal<<endl; }
(2). #include<iostream.h>
Struct stu{ int no; char name[10]; int marks;
void read(){cout<<“Enter the no,name,marks : “<<endl;
cin>>no>>name>>marks; }
void dis()
{ cout<<no<<endl<<name<<endl<<marks<<endl; }
};
Void main(){ stu s; s.read(); s.dis(); }
Union
 Its work same like a structure.
 Here it’s a possible to access only one member at a time.
 Example: #include<iostream.h> #include<string.h>
(1). Union abc{ int a; char ch; float x; };
Void main(){ abc p={‘k’}; //its not possible to assign two or more
union members like abc p={‘k’,90.0}
Cout<<p.ch; cout<<sizeof(p); }
Or,
Void main()
{ abc p;
Cout<<“Enter the no”<<endl; cin>>p.a; cout<<“\n”<<p.a<<endl;
Cout<<“Enter a character\n”; cin>>p.ch;
cout<<“\n”<<p.ch<<endl;
}
Enumeration
 Set of constants is called enumeration.
 Here ‘enum’ is the keyword.
 System assign default value to the constant starting from
0(zero) and ended from n-1.
 It’s a possible to assign name to constant value. By default first
name it will be stored zero, next name 1 and so on like….n-1.
 But it possible to change that value by some rule.
 Example: #include<iostream.h>
(1). enum emp{ramu, cmc,rani};
Void main(){ cout<<cmc; //cmc=cmc+3; this is constant so error. }
(2). Enum emp{ramu, cmc=45, rani};
Void main()
{ cout<<rani<<endl; emp rani=emp(20);
Cout<<rani; }
Class
 Class is a blue print or a template which
contain data and code.
 data:- variables.
 code:- function (member function)
 Its possible to create user defined data
types, which is accepts on the data using
class we can create any number of
objects similar type.
 Using a class the variables and functions
are members of a class.
 By default class members are private.
 Example: car, flower, man, etc.
 Syntax: class class_name{ private: int a;
public: void read(); };
Class cont….. Object
 Object is an instant of a class.
 It is a real time entities.
 Based on the class you can create any number of instance and
object of similar type. Without object it’s a possible to create a
class. Without class its not a possible to create object.
 Example: TATA indica is a object of car class. Rose is a object
of the flower class.
Class cont….. Access specifiers
 C++ providing three types of access specifiers : (1) private.
(2) public. (3) protected.
 (1). Private: By default class members are private. Suppose the
class members are private, they can be accessed only by the
member of the class. Where there are declared.
These members it’s a not to possible to access outside of
members.
 (2). Public: These access specifiers allows the member of the
class to be access outside of class also.
 (3).Protected: These members are accessed that class and
immediately next class member. Example: Base class member
accessed through derived class members into Inheritance.
Class …..Scope Resolution operator
 The global version of a variable can’t be accessed from within
the inner block.
 In C++ its provides scope resolution operator(::).
 :: variable_name.
 Example: #include<iostream.h>
int a=10;
void main(){ int a=20;
cout<<::a<<endl;
cout<<::a+a; }
 Implementation of a member function outside of a class.
 Syntax:
Ret_type class_name :: func_name(Parameter_List)
{ statement1; statement2;…………..;;;;;;;;;;;;;;;;
}
Class …..Array of Objects:
 Example: #include<iostream.h>
class emp{ private: int no; char name[10]; float sal;
public: void get(); void put(); };
Void emp :: get() {
cout<<“\nEnter the no, name, sal”<<endl;
cin>>no>>name>>sal; }
Void emp :: put() {
cout<<“no=“<<no<<“ name=“<<name<<“ sal=“<<sal; }
Void main()
{ emp e1[5]; int i;
for(i=0; i<5; i++) { e1[i].get(); }
for(i=0; i<5; i++) { e1[i].put; }
}
This pointer( or this ):
 Instant variable member function, parameters name are same
use ‘this’ pointer.
 Example :
Class test{ int a,b;
public:
void read(int a, int b){ this->a=a; this->b=b; }
void dis(){ cout<<a<<b<<endl; }
};
Void main(){ test ob; ob.read(90,34); ob.dis(); }

Note: ‘this’ pointer used to point class variable.


Objects As a Parameter :
 Class xx{ int a,b;
public:
void get(int a, int b){ this->a=a; this->b=b; }
void sum(xx x, xx y){ a=x.a+y.a; b=x.b+y.b; }
void dis(){ cout<<a<<b<<endl; }
};
Void main()
{ xx ob1, ob2, ob3;
ob1.get(30,60);
ob2.get(22,70);
ob3.sum(ob1,ob2);
Ob1.dis(); ob2.dis(); ob3();
}
Default Arguments :
 #include<iostream.h>
Class def{ int a,b,c;
Public:
Void read(int x=30; int y=90; int z=45);
Void dis();
};
Void def::read(int x, int y, int z)
{ a=x; b=y; c=z; }
Void def::dis(){ cout<<a<<b<<c; }
Void main(){ def p; p.read(); p.dis(); }
Constant Function :
 A member function with constant keyword specifies that the
function is a read only function that does not modify the object.
 Example: #include<iostream.h>
Class con
{ int a,b;
public:
void read()
{ cout<<“Enter the values”; cin>>a>>b; }
void dis() const
{ cout<<a<<b<<endl; }
};
Void main()
{ con ob; ob.read(); ob.dis(); }
Static Variable :
 Both data members and member functions can be made static.
 These static member can be called without instantiating the
object.
 Data members :
 All the objects of the class shares only one copy of a static data
member.
 All static variables are initialized zero when the first object is
initialized.
 It is visible with the class but life time is a entire program.
 It’s possible to declare character and float variable as static.
 Example: class sta{ static int count; int n; public:
Void getdata( int a){ n=a; count++}
Void putdata(){ cout<<“Count = “<<count<<“ n=“<<n; } };
Int sta::count;
void main(){ sta ob1, ob2, ob3; ob1.getdata(90); ob1.getdata(80);
ob1.getdata(34); ob1.putdata(); }
Static Member Function :
 We can also define static member function inside of a class.
 Static members don’t have ‘this’ pointer, this function directly
you can accessed.
 Syntax to call a function: class_name::func_name();
 Example: #include<iostream.h>
Class sta{ static int a;
Public:
static void dis(){ cout<<a; }
};
Int sta::a=90;
Void main()
{ sta::dis();
}
FRIEND FUNCTION :
 A Friend Function has access to all private and protected
members of a class.
 Advantages:
 It is useful when you overloading certain types of operators.
 Friend makes the creation certain Input / Output function easier.
 It is useful when two or more classes may contain members
that are interrelated to other parts of the program.
 Characteristics:
 It is not member of class.
 It can’t be invoked with object of any class.
 These must be defined outside the class.
 These will take objects as parameters.
 It can be declared under private or public.
Friend Function cont…..
 Example: #include<iostream.h>
Class fri
{ private: int a;
public:
void read(){ a=10; }
friend void dis( fri );
};
Void dis(fri x){ cout<<x.a<<endl; }
Void main()
{ fri ob;
ob.read();
dis(ob);
}
Friend Function cont…..

 Example2: #include<iostream.h>
Class B;
Class A{ int a;
public: void read(){ a=20; }
friend void sum( A, B );
};
Class B{ int b;
public: void get(){ b=30; }
friend void sum( A, B );
};
Void sum( A x, B y){ cout<<x.a + y.b; }
Void main(){ A ob1; ob1.read();
B ob2; ob2.get(); sum(ob1, ob2);
}
Constructor :
 Constructor is special member function unable an object to be
initialized immediately upon it creation.
 Constructor is special member function which has the same
name of class.
 Constructor does not have return type.
 Constructor has to be declare as a public member.
 Constructor can be overloaded like function.
 Constructor can be used to initialized as member of a class.
 Constructor invoke when an object is created. We can say
constructors are used to initialized an object.
 This initialization or invocation are constructor done at
immediately.
 Example: class add{ private: int n1,n2,n3;
public: add(); //Constructor
void input(); void sum(); void output(); };
Constructor Cont.. Default Constructor :
 A constructor does not have any parameters called as default
constructor.
 Example: #include<iostream.h>
Class cmc
{ int a;
public:
cmc(){ a=10; }
void dis(){ cout<<a<<endl; }
};
Void main()
{
cmc ob; ob.dis();
}
Constructor Parameterized
Cont..

Constructor :
 A constructor can accept input value through parameters is
called parameterized constructor.
 Example: #include<iostream.h>
Class para{ int a,b;
public:
para( int, int ); void sum();
};
Para :: para(int x, int y){ a=x; b=y; }
Void para :: sum(){ cout<<a+b<<endl; }
Void main()
{ para ob(37, 64); ob.sum();
}
Constructor Cont..Constructor Overloading :
 Like function overloading constructor can also be overloaded.
 Constructor is overloaded when the same constructor with
either different parameters list (or), different parameter type are
existing in the same class.
 So in a class more than one constructor can be excitable
provided that either parameter list or parameter type must be
different.
 Example: #include<iostream.h>
Class con{ int a, b; char x; public:
con(){ cout<<“This is default constructor.”; }
con( int x, int y){ a=x; b=y; }
con( char t){ x=t; }
void dis(){ cout<<x; or, cout<<a<<b; } };
Void main(){ con ob(); con ob1(30, 40); ob1.dis(); }
 How to copy one object data to another
object:
 Example: #include<iostream.h>
Class copy
{ int a;
public:
void read(){ a=20; }
void dis(){ cout<<a; }
};
Void main()
{
copy ob, ob2;
ob.read();
ob2=ob; ob2.dis();
}
Constructor Cont.. Copy Constructor :
 This is constructor which take input as another object of similar
type.
 Copy constructor is constructor which takes input as another
object which equal lent to represent.
 Xyz a; xyz x2(a); or, x2=a;
 Example: #include<iostream.h>
Class copy{ int x, y;
public: copy( int a, int b){ x=a; y=b; }
copy( copy &ob){ x=ob.x; y=ob.y; }
void dis() { cout<<x<<y; }
};
Void main()
{ clrscr(); copy ob(10,20); copy ob2(ob); // copy ob2=ob;
ob2.dis(); }
Destructor
 The destructor is similar to a constructor its remove the memory
allocation by an object, whenever an object requirements is
over.
 Destructor can be specified in a class using ‘ ~ ’ ( tild ) symbol.
 Syntax: ~classname () { ------statement is optional;-------}
 Constructor name and Destructor name is same as the
following syntax just we are declare in a program so that as
soon as object requirement is over destructor get invoke.
 Example: class des
{ public: des(){ cout<<Hello<<endl; }
~des(){ cout<<“ CMC “<<endl; }
};
Void main(){ clrscr(); des ob; }
How to get address of function
 Example: #include<iostream.h>
Int dis();
Int cmc();
Void main()
{
cout<<(void *)dis<<endl;
cout<<(void *)cmc<<endl;
}
int cmc()
{ stmt; stmt; ------------; }
int dis()
{ stmt; stmt; ------------; }
Operator overloading
 Operator overloading provides a flexible option for the creation
of new definitions for most of C++ operators.
 Syntax: ret_type classname :: operator #(argument_list)
{ stmt;}
“ operator ” : operator is a keyword.
“ # “ : is the operator to be overloaded.
 Operator function can be either member or non – member
function of the class.
 Operator function can almost friend function.
 Rules:
1. Only existing operators can be overloaded. ‘new ‘ operators
can not be created.
2. The overloaded operator must have at least one operand that
must be user – defined type.
Operator overloading cont…..
3. We can not basic meaning of operator.
4. We can not alter the basic precedence of an operator.
5. You can not change the number of operands it will take.
6. Some operators can not be overloaded :
______sizeof
________ . (dot operator)
________ .* (Member pointer operator)
________ :: (Scope Resolution operator)
________ ?:

You might also like