Post on 07-Apr-2018
8/4/2019 BASIC_Adv C++_I
1/24
Trailokya NayakMarch 2007
BASIC/Adv. C++ - I( Classes, Object, Abstraction, Encapsulation)
( Constructor, Destructor, Function Overloading, Inline Function, Static Member function, Friend Function, Friend Class)
8/4/2019 BASIC_Adv C++_I
2/24
Trailokya NayakMarch 2007
What is C++?
It does:
Object Oriented Programming.
Generic Programming.
Allows Procedural programming like C
8/4/2019 BASIC_Adv C++_I
3/24
Trailokya NayakMarch 2007
Concepts of C++:
Addition to C++ over CClass
Object
EncapsulationAbstraction
Data Hiding
Inheritance
Polymorphism
8/4/2019 BASIC_Adv C++_I
4/24
Trailokya NayakMarch 2007
Concepts of C++:
Addition to C++ over C:
Templates
STL
RTTI
Exception Handling
Dynamic binding
Function Overloading
Inline function.
8/4/2019 BASIC_Adv C++_I
5/24
Trailokya NayakMarch 2007
CLASS
Class is a UDD, in which the data members andmember functions can be in one place to representthe frame of an object and operations possible onthat object.
Class Example {
int a;
static int b;
Public:Example(): a(0) { }
};
8/4/2019 BASIC_Adv C++_I
6/24
Trailokya NayakMarch 2007
CLASS Class entities:
Data Members
Static
Global to all the objects of that class
Non-Static Local to that object only.
Member Functions
Static Member Function
Non-Static Member Function Const Member Function.
8/4/2019 BASIC_Adv C++_I
7/24Trailokya NayakMarch 2007
Object
Object are the basic entities of Object OrientedProgramming, in which the data values and its
behaviors are combined together.
Creating an object just like declaring a variable of a
datatype. For Ex:ClassName objName; // Takes memory for all
// non-static attributes.
8/4/2019 BASIC_Adv C++_I
8/24Trailokya NayakMarch 2007
Static Members Static Members can be:
Static Data Member
These are the properties of the class and initialized,
when the classes are loaded into the memory.
The static data members are defined outside the class.
Static Member Function
These functions are the properties of the class and
can be called through the class name.
These are allowed to access the static data members.
8/4/2019 BASIC_Adv C++_I
9/24Trailokya NayakMarch 2007
FRIEND
Function
Global Function as Friend
Member Function as Friend
Friend Class
Merits and Demerits of Friend Class in Object
Oriented designing.
8/4/2019 BASIC_Adv C++_I
10/24Trailokya NayakMarch 2007
Constructor/ Destructor
Types Of Constructor
Default Constructor
Default Valued Constructor
Parameterized Constructor
Copy Constructor
Dynamic Constructor
One type of destructor, by default provided by thecompiler and can be overridden by developer, ifrequired.
8/4/2019 BASIC_Adv C++_I
11/24Trailokya NayakMarch 2007
Constructor A constructoris a special method that describes
how an instance of the class (called object) isconstructed
Whenever an instance of the class is created, its
constructor is called. C++ provides a default constructorfor each class,
which is a constructor with no parameters. But, one
can define multiple constructors for the same class,
and may even redefine the default constructor
8/4/2019 BASIC_Adv C++_I
12/24
8/4/2019 BASIC_Adv C++_I
13/24Trailokya NayakMarch 2007
Inline Function
Inline function is the function, where the function is
not called, rather the body of the function is
replaced in the function invocation part.
It is request to the compiler to make replaced thecode inline..
8/4/2019 BASIC_Adv C++_I
14/24
8/4/2019 BASIC_Adv C++_I
15/24Trailokya NayakMarch 2007
Inheritance It is mechanism in which one class can reuse the
code of an existing class.Single Inheritance
Hierarchical Inheritance
Multiple Inheritance
Multi-level Inheritance
Hybrid Inheritance.
Visibility Mode of the Inheritance
Public InheritanceProtected Inheritance
Private Inheritance
8/4/2019 BASIC_Adv C++_I
16/24Trailokya NayakMarch 2007
C++ Syntax:
Basic Syntax is inherited from C.
Primitive data types Supported data types: int, long, short, float, double,
char, bool, and enum
The size of data types is platform-dependent
Basic statement syntax If-else
Nested If-else
Switch.. Case
For While
Do-while
8/4/2019 BASIC_Adv C++_I
17/24Trailokya NayakMarch 2007
C++ Syntax (Operators):
Basic expression syntax with operators from C. Types of Operators on the basis of operands
Unary Binary
Ternary
Types of Operators on the basis of the functionality Arithmetic Operator ( + , -, *, / )
Relational Operator ( < , , >=, ==, != )
Logical Operator ( &&, ||, !)
Conditional Operator ( ? : )
Bit-wise Operator ( &, |, ~, ^, )
Assignment Operator ( =, +=, -=, /=, *=, &=, |=, ^= etc.)
Increment/Decrement operator ( pre & post ++/--)
Special operator ( Coma, Sizeof etc)
8/4/2019 BASIC_Adv C++_I
18/24Trailokya NayakMarch 2007
C++ Syntax (Operators/Comments):
Operators supported in C++. :: (Scope Resolution Operator) Dynamic Memory Allocation operators
New Delete
Type cast operators const_cast static_cast dynamic_cast reinterpret_cast
Comments/Internal Documentation Single-line Commenting ( // ) Multi-line Commenting ( /* */ )
8/4/2019 BASIC_Adv C++_I
19/24Trailokya NayakMarch 2007
Operator Overloading
The concept of extending the capability of the
operator to work with User defined data type is
known as Operator Overloading.
Operator cannot be overloaded
sizeof, *, ->, ::, ?: .
8/4/2019 BASIC_Adv C++_I
20/24Trailokya NayakMarch 2007
Pointers/Reference
Pointers are variables, which can contain the addressof another variable
Single Pointer
Double Pointer
Multi Pointers
Operations with pointer variables.
Subtraction between pointers are possible but noaddition.
Multiplications/divisions are not possible
Increment/decrement are possible.
8/4/2019 BASIC_Adv C++_I
21/24Trailokya NayakMarch 2007
Pointers/Reference
Pointer to data member.
class A { public: int num;}; int A::*pdm = &A::num;
Function Pointer.
int (*funcName)(int)
Pointer to Member function.
int (ClassName::*funcName)(int)
8/4/2019 BASIC_Adv C++_I
22/24Trailokya NayakMarch 2007
Pointers/Reference
Pointer variable and const Pointer variableDatatype *ptrVar;
Datatype *const ptrVar;
Const datatype *ptrVar;
Const datatype *const ptrVar;
Datatype const *ptrVar;
Reference Variable.
Alias of existing variable
Reference as strict const pointers.
8/4/2019 BASIC_Adv C++_I
23/24Trailokya NayakMarch 2007
Function Call
Function Calling can be done in six ways:
Call by Value
Call by address/pointer
Call by reference
Call by const Reference
Call by const address/pointer
Call by const Value
8/4/2019 BASIC_Adv C++_I
24/24
The End