Class Class is a data type or user defined data type. It is a collection of members, these members are 2 types. Data members Member functions In object oriented application development data is represent in the form of object
class in c++ | Structure in ‘C’ |
It is a collection of variables and functions | structure is a collection of variables |
Contents is private, protected or public | Contents is public / global |
It provide security for data | It does not provide security for data |
It support polymorphism and inheritance | It does not support polymorphism and inheritance |
Syntax of class in C++ class class-type-name { private : data-members; member-functions; public : data-members; member functions; }
Variable declared inside class is called data member Data member define state of the object or value hold by object.
Function defined inside a class is called member function Binding data members with member function inside a class called as Member function define behavior of object or operation performed on the object Private, public and protected are called as access specifier. Default members of class are private Default members of structure are global/ public
Access specifier define visibility on accessibility of members declared within class. C++ provide 3 access specifiers
Default members of class are private Private member are accessible within class or members of the same class Private members are not accessible within non members on members of other classes. Data hiding in object oriented is achieved by declaring data members within class as private Protected Protected member access by members of same class and derived class Protected members cannot access by non members and members of other classes.
public These members access by members of some class, derived class, non members and members of other classes.
with in class | Non members | Member of derived class | Member of Other class | |
Private | Yes | No | No | No |
Protected | Yes | No | Yes | No |
Public | Yes | Yes | Yes | Yes |
Syntax Class –name object –name, object-name,…. class defined structure of object
# include <iostream.h> [ writing a class within main- local class triangle classes & vice versa] { private : float base, height; // data members public : void read() { cout<<”\n input base height”; cin>>base>>height; } void find area () { float area=0.5*base*height; }; Size of the class is equal to some of data member size On creation of object memory is allocated for data members of class but not member functions Member functions operators on one or more than one object.
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/15
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox