Data Structure Through C++

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

C++ Class Overview.

Class is similar to a structure in C C++ is a subset of C Class and Objects are main idea of object oriented programming tools. Class is similar to a structure data type but it contains not only data element but also function which operates on data elements. The runtime system creates each object based on a class definition. A class is defined with the keyword class. A class is way to bind the data and its associated functions together. Its allows the data to be hidden if necessary from external use. Class specification has two parts. 1) Class declaration. 2) Class function definitions. The class declaration describes the type and scope of its members. The class function definitions describe how the class functions are implemented. IMPLEMENTATION OF A CLASS Implementation of a class is its inside view that encompasses the secrets of its behaviors. Interface of the class is divided into following parts. PUBLIC PROTECTED PRIVATE : part that is accessible to all clients. : part that is accessible to the class only. : part that is accessible to the class only.

BASIC OOP CONCEPTS 1) 2) 3) 4) 5) 6) 7) Abstraction. Encapsulation. Data hiding. Inheritance. Polymorphism. Dynamic binding. Massage passing.

Abstraction: Abstraction refers to the act of representing essential features without including the background details. Encapsulation: The wrapping up of data and function into a single unit is known as encapsulation. Data hiding; Data hiding deals with visibility of the members the level of visibility determines which parts of the code can access the member. Three levels of visibility 1) Public 2) Protected 3) Private Inheritance: The inheritance provide the idea of reusability. This means that we can add additional features to an existing class. This is possible by deriving a new class from existing class. New class contain both the classes. Polymorphism: Polymorphism means the ability to take more then one form Dynamic Binding: Binding refers to the 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 run time. Massage passing: OOPs consist of set of object that communicates with each other. The process of programming in an object oriented language, involves following basic steps. 1) Creating classes that define objects and their behavior. 2) Creating objects from class definition. 3) Establishing communication among objects.

1.3 CLASS DEFINITIONS A class definition is a process of naming a class and data variables, and methods of the class. A class is a way to bind the data and its associated functions together. It allows the data to be hidden, if necessary from external use. Class if define as a set of objects. The variables declared inside the class are known as data members and the functions are knows as member functions.

Example: Class student { Private: int number; int age; public: void get_info(); void put_info(); } 1.4 OBJECTS The class variables are known as objects. These are basic run time entities in an object oriented programming . The syntax for defining objects of a class is as shown below: Class class_name object1,object2 .object n; Where as class is the keyword class_name is the name of the user defined class and object 1, object 2.. . object n are user define objects Example : Class student S1; Or Student S1; Its creates the object S1 of the class student and also creates more then one object.

Class student S1,S2,S3; Its create multiple objects of a class 1.5 CLASS MEMBERS The variable and functions that are declared within the class body are called class member. Class member

Variables (Data member) ))

Function (member function)

The visibility labels called private, public and protect; Example: Class student { Public: Private; Protected: }; Public member : all data member and function declared in the public section of the class can be accessed an where in the program either by function of the class or by the external to the class. Private member : only the member functions of the same class can access the private members. The private members of a class are inaccessible outside the class. Protected member : protected members access control is similar to that of private members. It can access only derived class. 1.6 ACCESS CONTROL Every data member can be preceded with the access specifier that indicates the scope of the data member. An access specifier is used to modify the access rights of the members of the class. Three access specifiers used in c++ are Private : if a class member is member is declared as private then it is accessible only to the member functions declared in that class and by the friend functions of that class. // public assessable member function. // members accessible to other members of the class //member accessible to derived class only // declare the student class

Protected: if a class member is declared as protected then it can be accessed by 1) The member functions of the class in which it is declared 2) The friend function of the class 3) The member functions and the friend functions of the derived class. Public : all data member and function declared in the public section of the class can be accessed an where in the program either by function of the class or by the external to the class. 1.6 CLASS SCOPE:

You might also like