Você está na página 1de 24

BASIC/Adv.

C++ - I
( Classes, Object, Abstraction, Encapsulation)
( Constructor, Destructor, Function Overloading, Inline Function, Static Member function, Friend Function, Friend Class)

March 2007

Trailokya Nayak

What is C++?
It does:
Object Oriented Programming. Generic Programming. Allows Procedural programming like C

March 2007

Trailokya Nayak

Concepts of C++:
Addition to C++ over C Class Object Encapsulation Abstraction Data Hiding Inheritance Polymorphism

March 2007

Trailokya Nayak

Concepts of C++:
Addition to C++ over C: Templates STL RTTI Exception Handling Dynamic binding Function Overloading Inline function.

March 2007

Trailokya Nayak

CLASS
Class is a UDD, in which the data members and member functions can be in one place to represent the frame of an object and operations possible on that object. Class Example { int a; static int b; Public: Example(): a(0) { } };
March 2007

Trailokya Nayak

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.

March 2007

Trailokya Nayak

Object
Object are the basic entities of Object Oriented Programming, 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.

March 2007

Trailokya Nayak

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.

March 2007

Trailokya Nayak

FRIEND
Function Global Function as Friend Member Function as Friend Friend Class Merits and Demerits of Friend Class in Object Oriented designing.

March 2007

Trailokya Nayak

Constructor/ Destructor
Types Of Constructor Default Constructor Default Valued Constructor Parameterized Constructor Copy Constructor Dynamic Constructor One type of destructor, by default provided by the compiler and can be overridden by developer, if required.

March 2007

Trailokya Nayak

Constructor
A constructor is a special method that describes how an instance of the class (called object) is constructed Whenever an instance of the class is created, its constructor is called. C++ provides a default constructor for 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

March 2007

Trailokya Nayak

Destructor
A destructor is called when an object is deleted either implicitly, or explicitly (using the delete operation)
The destructor is called whenever an object goes out of scope or is subjected to a delete. Typically, the destructor is used to free up any resources that were allocated during the use of the object

C++ provides a default destructor for each class


The default simply applies the destructor on each data member. But we can redefine the destructor of a class. A C++ class can have only one destructor. One can redefine the destructor of a class.

A C++ class can have only one destructor

March 2007

Trailokya Nayak

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 the code inline..

March 2007

Trailokya Nayak

Function Overloading
A set of function having the same name and different argument list is known as Function Overloading. int FunctionName( int, float); float FunctionName( int, float);
Whether they are overloaded.

March 2007

Trailokya Nayak

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 Inheritance Protected Inheritance Private Inheritance
March 2007

Trailokya Nayak

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

March 2007

Trailokya Nayak

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)

March 2007

Trailokya Nayak

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 ( /* */ )

March 2007

Trailokya Nayak

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, *, ->, ::, ?: .

March 2007

Trailokya Nayak

Pointers/Reference
Pointers are variables, which can contain the address of another variable Single Pointer Double Pointer Multi Pointers Operations with pointer variables. Subtraction between pointers are possible but no addition. Multiplications/divisions are not possible Increment/decrement are possible.

March 2007

Trailokya Nayak

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)

March 2007

Trailokya Nayak

Pointers/Reference
Pointer variable and const Pointer variable Datatype *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.

March 2007

Trailokya Nayak

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

March 2007

Trailokya Nayak

The End

March 2007

Trailokya Nayak

Você também pode gostar