Published on

Published in: Education, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. CLASS & OBJECTS A class is a way to bind the data & its associated functions together. Class is the basic concept of object oriented program and it provides data hiding. Generally class specification has two parts .● Class declaration : describes type & scope of its members.● Class function definition : describes how the class functions are implemented
  2. 2. The general form of a class declaration isclass class_name{private:---------------variable declarations;---------------function declarations;public:---------------variable declarations;---------------function declarations;};
  3. 3. Once a class has been defined ,we can create any no of objects belonging to that class . Each object is associated with the data of type class with which they are created . For Example : fruit mango class object● Objects are the basic run time entities in an object oriented system.● Objects are used to invoke a class .when a program is executed objects interact by sending message to one another.
  4. 4. The functions and variables are collectively called class members. They are usually grouped under two sections namely “private” and “public” to denote which of the members are private and which of them are public. These are known as visibility labels(access modifiers) .● The class members declared as private can be accessed only from with in the class.● The class members declared as public can be accessed from out side the class also.● The use of keyword private is optional . By default the members of the class are private.
  5. 5. If both the labels are missing ,then by default ,all the members are private .such a class is completely hidden from the outside world. Access specifiers● Private: private members of a class are visible only with in the class● Public: public members of class are visible to all● Protected: protected members of a class are visible with in the class as well as in the immediately derived class
  6. 6. ClassNoentry to Private areaprivatearea Data Functions DATA HIDING IN CLASS public area Data Entry Functions allowed to public area
  7. 7. Visibility of a member in inheritance● Inheritance by private When a base class is privately inherited by a derived class , the public members of the base class become private members of the derived class and therefore the public members of the base class can only be accessed by the member functions of the derived class . They are inaccessible to the objects of the derived class .
  8. 8. ● Inheritance by public When the base class is publicly inherited, the public members of the base class become public members of the derived class and therefore they are accessible to the objects of the derived class .● Inheritance by protected c++ provides a third visibility modifier protected,which serve a limited purpose in inheritance. A member declared as protected is accessible by the member functions with in its class and any class immediately derived from it. It can not be accessed by the function
  9. 9. outside these two classes. A class can nowuse all the three visibility modes as follows,class alpha{private: optional----------------; visible to member functions within its class----------------;protected:----------------; visible to member functions of its own & derived----------------; classpublic:----------------; visible to all functions in the program----------------;
  10. 10. Visibility of inherited membersBase class Derived class visibilityvisibility Public Private Protected derivation derivation derivationprivate Not inherited Not inherited Not inheritedprotected protected private protectedpublic public private protected
  11. 11. Example : private derivationclass stud class science:private student{ {int rlno ; char name[20]; int math,sci;public: public:void getdata() void getmarks(){ {cout<<”ENTER ROLL NO:”; stud::getdata(); //calling base class functioncin>>rlno; cout<<”ENTER MARKS IN MATHS & SCIENCE:”;cout<<”ENTER NAME:”; cin>>math>>sci;cin>>name; } }void putdata() void putmarks(){ {cout<<”ROLL NO:”<<rlno; stud::putdata(); //calling base class functioncout<<”NAME:”<<name; cout<<”MARKS IN MATHS:”<<math<<”& SCIENCE:”<<sci;} }; } };
  12. 12. void main() { science s1; s1.getdata(); public members which privately derived is inaccessible s1.getmarks(); s1.putdata(): public members which privately derived is inaccessible s1.putmarks }Output :ENTER ROLL NO :10ENTER NAME :SHYAMENTER MARKS IN MATHE & SCIENCE :90 95ROLL NO : 10NAME : SHYAMMARKS IN MATHS IS 90 & SCIENCE IS 95
  13. 13. THANK YOU ! BY HARI