cpp notes11.doc

21
CPP NOTES ----------------- C language is developed from the concept of POPS which means we can build any application with collection of functions. In POPS these functions can share global data that’s why there is no security from that data. At the time of developing c language they are concentrating on functions only but not about data. CPP is developed from OOPS (Object Oriented Programing) technology here data is represented as objects. Any global function cannot access the data of object. So, here data and code were encapsulated into a single unit. We can define this object through class type. FEATURES OF POPS(Procedure Oriented programming structure):- ------------------------------------------------------- ------------------------------ 1. Emphasis is on doing things(Algorithms) 2. Large programs are divided into smaller programs known as functions. 3. Most of the functions share global data. 4. Data move openly around the system from function to function. 5. Functions transform data from one form to another form. 6. Employees top down approach in program design. FEATURES OF OOP’S(Object oriented programming structure):- 1

Upload: srinusunkara

Post on 01-Oct-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

CPP NOTES -----------------C language is developed from the concept of POPS which means we can build any application with collection of functions. In POPS these functions can share global data thats why there is no security from that data. At the time of developing c language they are concentrating on functions only but not about data.

CPP is developed from OOPS (Object Oriented Programing) technology here data is represented as objects. Any global function cannot access the data of object. So, here data and code were encapsulated into a single unit. We can define this object through class type.

FEATURES OF POPS(Procedure Oriented programming structure):-

-------------------------------------------------------------------------------------1. Emphasis is on doing things(Algorithms)

2. Large programs are divided into smaller programs known as functions.3. Most of the functions share global data.

4. Data move openly around the system from function to function.

5. Functions transform data from one form to another form.6. Employees top down approach in program design.

FEATURES OF OOPS(Object oriented programming structure):-

---------------------------------------------------------------------------------1. Emphasis is on data rather than procedure.

2. Programs are divided into smaller what are known as objects.

3. Data structures are designed such that they characterized the objects.

4. Functions that operate on the data of an object are tied together in the data structure.5. Data is hidden and cant be accessed by external functions.

6. Objects may communicate with each other through functions.

7. New data and functions can be easily added whenever necessary.

8. Follows bottom up approach in program design.

--------------------------------------------------------------------------------------- CONCEPTS OF (OOPS) OBJECT ORIENTED PROGRAMMING

----------------------------------------------------------------------------------------1. OBJECTS.2. CLASSES.3. Data abstraction.4. Inheritance.5. Polymorphism.6. Operator Overloading7. Templates

8. Inline function

9. Static Data Member

10. Friend Function.OBJECTS:-

-------------

Objects are the basic runtime entities in the object oriented system. objects are the variables of type class.

CLASS:-

--------------Object contain data and code to manipulate that data. The entire set of data and code of an object can be made a user defined type with the help of a class.

DATA ENCAPSULATION:-

-------------------------------------The wrapping up of the data and functions into single unit is known as encapsulation. The data is not accessible to the outside world can only those functions which are wrapped in the class can access it.

DATA ABSTRACTION:-

---------------------------------Abstraction refers to the act of representing essential features of without including the background details or explanations. Classes uses the concept of abstraction and are defined as list of abstract attributes such as size, weight and cost, and functions to operate on these attributes.

INHERITANCE:-

-------------------

Inheritance is process by which objects of one class are acquire the properties of objects of another class.

POLYMORPHISM:-

---------------------------Polymorphism is another important oops concept It means the ability to take more than one form.

DYNAMIC BINDING:-

-------------------------

Binding refers to linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at runtime. It is associated with polymorphism and inheritance.

MESSAGE COMMUNICATION:-

---------------------------------------------An object oriented program consist of a set of objects that communicate with each other the process of programing is an object oriented language therefore involve the following steps

1. creating classes that define objects and their behaviour

2. Creating objects from class definition.

3. Establishing communication among objects.

------------------------------------------------------------------------------------

CLASS SYNTAX

-------------------------class

{

private:

//member variables

//member functions

public:

//member variables

//member functions

};

DESCRIPTION:-

-----------------------In c++ by using class key word we can define user defined data type that works like a built-in type. Private and public are two visibility modes inside the class.

PRIVATE:-

----------------Member variables or member functions are defined under private can be accessed by the inside member functions only.PUBLIC:-

-------------Member variables or member functions defined under public visibility mode can be accessed from outside of the class by using class variables (objects) and dot membership operator.

------------------------------------------------------------------------------------

INLINE FUNCITONS

------------------------------This is the keyword inline is used as a function specifier only in function declarations the specifier is a hint to the compiler that inline substitution of the function body

is to be performed to the usual function call implementation. The advantages of using a inline functions are

1. The size of object code is considerably reduced.

2. It increases the execution speed.

3. The inline functions are compact functions calls whenever

a function declared with the inline specifier the c++ compiler replaces it with the code itself so the overhead of the transfer of control between the calling portion of a program and function is reduced. The inline specifier can be used either a member of a class or global function.

------------------------------------------------------------------------------------

FRIEND FUNCTIONS

-------------------------------If we want to make any global function friend to the class that function prototype must defined inside the class using key word friend.Properties:

--------------

1. A friend function can access the private data of the class through objects.

2. Its not in the scope class.

3. Since its not in the scope of the class these functions are not called through objects.

4. Generally friend functions are taking class objects as arguments.

------------------------------------------------------------------------------------

CONSTRUCTORS

--------------------------CONSTRUCTOR:-

-------------------------Constructors are the special functions whose task is initializing the objects. Constructors are special means, function name and class name must be same.

PROPERTIES: -

1. Constructors should not specify any return type and should not contain any return statements.

2. Constructors must be defined in public section only.

3. Constructors executed automatically while creating objects.4. Constructor functions may be called explicitly also.Types of Constructors:-

1. Default constructor

2. Argument constructor

3. Copy constructor

4. Overloaded constructor

DESTRUCTORS

Destructors are the functions which are invoked implicitely whenever the program control goes out of scope. Destructor name and class name must be same and preceded by tilde symbol (~) If we are allocating memory for any object using new key word those memory blocks must be deleted by delete key word.

------------------------------------------------------------------------------------

OPERATOR OVERLOADING

------------------------------------------Operator overloading is one of the many exciting features of C++. Its an important technique that has enchased the power of extensibility of C++. Operator overloading means to define additional task to an operator. We must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function called operator function. which describes the task. The general form of an operator function:-

Return type class name :: operator op(args-list)

{

//function body

}Void addition :: operator +(int a, int b)Rules for overloading operator:-

------------------------------------------Only existing operators can be overloaded new operators cannot be overloaded. The overloaded operator must have at least one operand (a++) that is of user defined type. We cannot change the basic meaning of an operator. That is we cannot redefine the plus(+) operator to subtract one value from other. Overloaded operators follow the syntax rules of the original operators. That cannot be overhidden. There are some operators they cannot be overloaded.5.1 class member access operators(.,.*)

5.2 scope resolution operator(::)

5.3 size operator (sizeof)

5.4 Conditional operator(? :)

Unary operators, overloaded by means of a member function, take no explicit arguments and return to explicit value. But they overloaded by means of a friend function take one reference argument (the object of the relevant class) Binary operators, overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments. When using binary operators overloaded through a member functions the left-hand operand must be an object of the relevant class Obj++;. Binary arithmetic operators such as +,-,* and / must be explicitly return a value. They must not attempt to change their arguments. We cannot use friend functions to overload certain operators however member functions can be used to overload them.

They are-------

=Assignment operator

() Function call operator

[ ] subscripting operator

-> class member access operator

FUNCTION OVERLOADING

-------------------------------

In c++ we can define same function name no of times by defferenciating with no of arguments and type of arguments while compiling the compiler matches the function name with no of arguments and type of arguments. this is the concept of polymorphism

------------------------------------------------------------------------------------

STATIC MEMBERFUNCTIONS

---------------------------

Member functions may be defined as static.

properties:

-----------

Static functions can access only static data.

Static functions can be called without creating object classes.

Static functions are called by using to class name and scope resolutions.

STATIC DATA MEMBERS

--------------------------------

A data member of a class can be qualified as a static .The properties of a static member variable are similar to that of a 'c' static variable.

(1).It is initialized to zero when the first object of its class is created. No other initialization is permitted.

(2).Only one copy of that member is created for the entire class is stored by all the objects of that class nominates how many objects are created.

(3).It is visible only with in the class. But its lifetime is the entire program. Static variables are normally used to maintain values common to the entire class.

---------------------------------------------------------------------------------------------------- INHERITANCE

------------------------C++ supports the concept of reusability. The c++ classes can be reused in several ways. Once a class has been written and tested. This is basically done by creating new classes, reusing the properties of the existing ones. The mechanism of deriving a new class from an old one is called the derived class. The derived class inherits some of all of the properties of the base class. A class can also inherit properties from more than one class or from more than one level.A derived class with one base class is called SINGLE INHERITANCE.And one with several base classes is called MULTIPLE INHERITANCE. The properties of one class may be inherited by more than one class. This process is HIERARCHIAL INHERITANCE. The mechanism of deriving a class from another derived class is known as MULTI LEVEL INHERITANCE.Syntax:

class :

{

// statements;

}

Class ABC

{

private: int a;

public: int b;

protected: int c;

};

class XYZ : private ABC

{

private: Int b;

int c;

protected:

public:

}Access specifies:

---------------------

public :

private :

protected:

Public:

---------

If a member is public, it can be used by any function.Private:

---------

If a member is private, it can only used by member functions and friends of the class in which it is declared. Members of a class are private by default.Protected:

------------

If a member is protected, it access is the same as for private. In addition, the member can be used by member functions and friends of classes derived from declared class, but only in objects of the derived type.

-------------------------------------------------------------------------------------

VIRTUAL BASE CLASS

--------------------------------- Grand parent

parent1 parent2

CHILD

The child has 2 direct base classes parent1 and parent2 which themselves have a common base class grandparent. The child inherits all the traits of grandparents via two Separate paths. All the public and protected members of grandparent are inherited into child twice; first via parent 1 and parent 2 this means child would have duplicate sets of the members inherited ambiguity and should be avoided. The duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual base class.While declaring the direct or intermediaete base class.

----------------------------------------------------------------- THIS POINTER

-----------------------C++ uses a unique keyword called this to represent an object that invokes a member function. This is a pointer that points to the object for which this function was called.

For example the function calls A-max () will set the pointer this to the address of the object A.

This unique pointer is automatically passed to a member function when it is called The pointer this acts as an implicit argument to all the member functions.

Class ABC

{

int a;

------- -------

};

The pointer variable a can be used directly inside a member

function, like----------

a=123;

We can also use the following statement to do the same job;

this->a=123;

-----------------------------------------------------------------------------

PURE VIRTUAL FUNCTIONS

-------------------------------------------It is a normal practice to declare a function virtual inside the base class and redefine it in the derived classes. The function inside the base class is seldom used for performing any task It only acts a place holder. For example we have not defined any object of the class has been defined 'empty'. such functions are called "do nothing" functions. A "donothing" function may be defined as follows. Virtual

void display( )=0;

Such functions are called pure virtual functions. A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such cases the compiler requires each derived class to either define the function or redeclared it as a pure virtual function. Remember that a class containing pure virtual function cannot be used to declare any objects of its own such classes are called abstract base class.

-----------------------------------------------------------------------------

POLYMORPHISM

--------------------------Polymorphism is one of the crucial features of oops. It means simply one name, multiple forms. We have already seen how the concept of polymorphism is implemented using the overloaded functions and operators. The overloaded member functions are selected for invoking by matching arguments both type and number. This information is known to the compiler is able to select the appropriate function for a particular call at the compile time itself. This is called early binding or static linking. also known as compile time polymorphism. Early binding means that an object is bound to its functional at compile time.

For ex:

Class A

{

int x;

public:

void show( ) { ----------- } //show in base class

};

class B : public A

{

int y;

public:

void show( ) { ------------------ }//show() in derived class

};

-----------------------------------------------------------------------------

POINTERS TO DERIVED CLASS:----------------------------------------------If B is a base class and D is a derived as a pointer to B can also be a pointer to D

B *cptr;

B b;

D d;

cptr=&b;

We can make cptr to point to the object d as follows: -cptr=&d;

This is perfectly valid with c++ because d is a object

Derived from the class B

However there is a problem in using cptr to access the public members of the derived class D. using c ptr, we can access only those members inherited from B and not the members that originally belong to D. In case a member of D has the same name as one of the members of B, then any reference to that member by cptr will always access the base class member.

-----------------------------------------------------------------------------

TEMPLATES

-----------------Template key word constructs a family related functions or classes

Syntax:

Template - declaration

Templatedeclaration Function templates:

Consider a function max(x, y) that returns the larger of its two arguments. X and Y can be of any type that has the ability to be ordered. Because C++ is strongly typed language, it expects the type of parameters x and y to be declared at compile time. Without using templates, may overloaded versions of max() are required(one for each data type to be supported), even though the code for each version is essentially identical. Each version compares the arguments and returns the larger. By using a template, you can define a pattern for a family of related overloaded functions by getting type be a parameter.---------------------------------------------------------------------------------------------------- MANIPULATORS

--------------------------These functions are special stream functions they change certain characters of input and output. They change the format flags and values for a stream.

The main advantage of using manipulator functions is that they facilitate the formatting input and output streams. These manipulators are placed on the header file known as "iomanip.h".(1).endl (2) hex, dec, Oct (3) setw (4) setfill (5) Setprecision

--------------------------------------------------------------------------- DYNAMIC MEMORY ALLOCATION

--------------------------------------------------New and delete operators:

---------------------------------

The operator that creates and destroys an object.

Syntax:-

-----------=new[ ];

-->delete

The "new" operator tries to create an object < name > by allocating the size of ( < name > ) bytes in the heap.

The delete operator destroys the object < name > by deallocating size of ( < name > ) bytes [pointed to by ).The storage duration of the new object is from the point of creation until the operator "delete" deallocates the memory or untill the end of program.

------------------------------------------------------------------------------------

4