C Language
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.
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.
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)
________ ?: