C++_Notes

89
- 1 - C++ (C with Classes) Object Oriented Programming (OOP) The term ‘‘Object Oriented Programming” means a programming language that fully supports the object oriented (features) style of programming. Introduction C++ is an object-oriented programming language developed by Bjarne Stroustrup at AT & T Bell Labs in the early 1980’s. Since the class concept was the major addition to the original C language, it is called as "C with classes". However later in 1983, the name was changed to C++. The idea of C++ comes from the 'C' increment operator "++", there by suggesting that C++ is an incremented version of 'C'. C++ is a superset of C. Therefore almost all C programs are valid C++ programs. Difference between C & C++ ‘C’ is a procedure-oriented programming language where everything is placed in the form of procedures or functions. While we concentrate on functions very little attention is given to the data that are being used by various functions & any function can access the information provided in other functions. It doesn’t model the real world problems very well. It employs top-down approach in program design.

description

hj

Transcript of C++_Notes

Difference between C & C++

- 1 -

C++ (C with Classes)Object Oriented Programming (OOP)

The term Object Oriented Programming means a programming language that fully supports the object oriented (features) style of programming.Introduction

C++ is an object-oriented programming language developed by Bjarne Stroustrup at AT & T Bell Labs in the early 1980s.Since the class concept was the major addition to the original C language, it is called as "C with classes". However later in 1983, the name was changed to C++. The idea of C++ comes from the 'C' increment operator "++", there by suggesting that C++ is an incremented version of 'C'. C++ is a superset of C. Therefore almost all C programs are valid C++ programs.Difference between C & C++

C is a procedure-oriented programming language where everything is placed in the form of procedures or functions. While we concentrate on functions very little attention is given to the data that are being used by various functions & any function can access the information provided in other functions. It doesnt model the real world problems very well. It employs top-down approach in program design.

Where as C++ is an object-oriented programming language which treats data as a critical element in the program and it doesnt allow moving freely around the system. It binds the data & the functions that operate on data together and protects it from accidental use of other functions. OOP allows us to decompose a problem into a number of entities called objects and built data and functions around these objects.Advantages of OOP

1. Object Oriented Approach

2. Closer to real world object

3. Dynamic Declaration

4. Data Security

5. Code Re-Usability & ExtensibilityBasic concepts of Object-Oriented programming languageClassA class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.A class is a logical construct of an object.A class is an abstract representation of something, whereas an object is a usable example of the class.A class is an expanded concept of a structure in C, instead of holding only data; it can hold both data and functions. Once a class has been defined, you can create any number of objects based on that class.Object

An object is an instance (example or illustration) of a class. In terms of variables, a class would be the data type and an object would be the variable.An object is a physical reality.An object is a software bundle of related variables and methods. Software objects are often used to model real-world objects you find in everyday life.Polymorphism (many forms)

An ability to have more than one function with the same name, each having different functionality. The function going to execute is determined at run time. Polymorphism refers to the fact that a single operation can have different behavior in different objects.E.g.: Overloading, Overriding.Overloading:

Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called.Inheritance

The mechanism of creating new classes by deriving the features from existing classes is called Inheritance.In OOP the concept of inheritance provides the idea of reusability & extensibility. This means that we can add additional features of an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes.Encapsulation

The method of packing all the data members & member functions into a single unit called class and the process is called EncapsulationData Hiding

The process of hiding the data members and member functions from access by the other class objects other than the same class objects.The data is not accessible to the outside world and only those functions which are placed in the class can access it, these functions provide the interface between objects data and the program. The insulation of data from direct access by the program is called data hiding.

Data AbstractionIt is the process of accessing the data members & member functions with the objects.

Abstraction refers to the act of representing essential features without including the background details and explanations. Classes use the concept of abstraction and defined as a list of abstraction attributes. Since the classes use the concept of data abstraction they are known as Abstract Data Type (ADT).Data Binding

Binding refers to the act of associating an object or a class with its member. If we can call a method fn() on an object o of a class c, we say that the object o is binded with the method fn(). This happens at compile time and is known as static or compile - time binding.The calls to the virtual member functions are resolved during run-time. This mechanism is known as dynamic binding. The most prominent reason why a virtual function will be used is to have a different functionality in the derived class. The difference between a non-virtual member function and a virtual member function is the non-virtual member functions are resolved at compile time.Structure of C++ program:

Include files

Class declaration

Member functions definition

Main function

{

Local declaration;

Object declaration;

Executable coding;

}

Syntax for Class Declaration

class class_name

{

access_specifier_1:

data members;

access_specifier_2:

member functions;

.

} object_names;

where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, which can be either data or function declarations, or optionally access specifiers. All is very similar to the declaration on structures, except the new thing called access specifier. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire:

private members of a class are accessible only within the class and by the public members of the same class or from their friends.

protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.

public members are accessible from anywhere in the program where the object is visible.

By default, all members of a class declared with the class keyword have private access for all its members.

Function Definitions outside the class

Member functions that are declared inside a class have to be defined separately outside the class. They should have a function definition outside the class asreturn_type class_name :: function_name(arguments list)

{

function body;

}

The membership label class_name :: tells the compiler that the function belongs to the class class_name. That is the scope of the function is restricted to the class_name specified in the header line.

The symbol "::" is called Scope Resolution operator.The scope operator (::) specifies the class to which the member being declared belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition.The only difference between defining a class member function completely within its class and to include only the prototype and later its definition, is that in the first case the function will automatically be considered an inline member function by the compiler, while in the second it will be a normal (not-inline) class member function, which in fact supposes no difference in behavior.Inline Function

They are shortcut functions that are not actually called; rather their code is expanded at the point of execution. If a function is declared with the keyword inline, the compiler does not call the actual function instead it copies the code from the inline function directly into the calling function.Inline is a hint to the compiler that you would like the function to be inlined. The compiler is free to ignore the hint and make a real function call.this pointer:

"this" is a pointer that points the address of the current object. The 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.Object Arrays

We know that an array can be of any data type including struct; similarly we can also have arrays of variables that are of the type class. Such variables are called arrays of objects.

class employ

{

char name[10];

int age;

public:

void inputdata();

void outputdata();

};

ex:-

employ emp[5];Create A Class With The Name Bank And Create An Array With 5 Cells And Accept Data And Print Data.

Example for an Object Arrays.

# include

# include

# include class bank

{

int acno,cbal;

char cname[20];

public:

void input();

void output();

};

void bank::input()

{

coutacno;

coutcname;

coutcbal;

}

void bank::output()

{

cout