Presentation is loading. Please wait.

Presentation is loading. Please wait.

CLASSES AND OBJECTS.

Similar presentations


Presentation on theme: "CLASSES AND OBJECTS."— Presentation transcript:

1 CLASSES AND OBJECTS

2 Class A class is an organization of data and functions which operate on them. Data structures are called data members and the functions are called member functions. The combination of data members and member functions constitute a data object or simply an object.

3 Object Instantiation of a class.
In terms of variables, class would be the type and an object would be a variable.

4 General Structure of a class
Class name or name of class Data Members Member functions Access Specifiers Declaring objects

5 Classes in C++ A class definition begins with the keyword class.
The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. }; Any valid identifier Class body (data member + methods)

6 class classname { private: variable declarations; function declarations; public: protected: } obj1, obj2,…..objN;

7 Class name Name given to a particular class.
Serves as a name specifier for the class using which we can create objects. The class is specified by keyword “class”

8 Data Members Data type properties that describe the characteristics of a class. We can declare any number of data members of any type in a class. We can say that variables in C and data members in C++. E.g. int rn;

9 Member functions Various operations that can be performed to data members of that class. We can declare any number of member functions of any type in a class. E.g. void read();

10 Access Specifiers Used to specify access rights for the data members and member functions of the class. Depending upon the access level of a class member, access to it is allowed or denied. Within the body, the keywords private: and public: specify the access level of the members of the class. the default is private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

11 Classes in C++ class class_name { private: …
public: }; private members or methods Public members or methods

12 Private members and methods are for internal use only.
only members of that class have accessibility can be accessed only through member functions of that class. Private members and methods are for internal use only.

13 Public: Accessible from outside the class can be accessed through member function of any class in the same program.

14 Protected: Stage between private and public access. They cannot be accessed from outside the class, but can be accessed from the derived class.(inheritance)

15 Class Example This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; They are accessible from outside the class, and they can access the member (radius)

16 C++ supports three access specifiers:
public private protected The public access specifier allows a class to subject its member variables and member functions to other functions and objects The private access specifier allows a class to hide its member variables and member functions from other class objects and functions The protected access specifier allows a class to hide its member variables and member functions from other class objects and functions just like private access specifier - is used while implementing inheritance

17 A class declaration only uses to build the structure of an object.
Declaring objects: A class declaration only uses to build the structure of an object. Declaration of an object is same as declaration of class. Defining objects of class data type is known as class instantiation(instances of class) When we create objects during that moment , memory is allocated to them. Ex- Circle c;

18 class Customer { void accept() cout << “Accepting Customer Details” << endl; } void display() cout << “Displaying Customer Details” << endl; }; Void main() Customer C1; C1.accept(); C1.display(); getch();


Download ppt "CLASSES AND OBJECTS."

Similar presentations


Ads by Google