C++ is a OOP Version of C…
A class is a blueprint of an object…..Basically a object is created from class
c++ class define:
class Box{ public: double length; //length of the Box double breadth; //breadth of the Box double height; //Height of the Box };
c++ object define:
Here we are declaring two objects of a class Box
Box box1; // declare box1 of type Box
Box box2; //declare box2 of type Box
Basic example of OOP here :
#include<iostream> using namespace std; class Box{ public: double length; double breadth; double height; }; int main() { Box box1; Box box2; double volume=0.0; //box1 specification box1.height=10.0; box1.length=6.0; box1.breadth=7.0; //box2 specification box2.height=10.0; box2.length=12.0; box2.breadth=13.0; volume=box1.height*box1.length*box1.breadth; cout<<"Volume of Box1 = "<<volume<<endl; volume=box2.height*box2.length*box2.breadth; cout<<"Volume of Box2 = "<<volume<<endl; return 0; }
C++ Class Member Functions:
#include<iostream> using namespace std; class Box{ public: //You have to define the privacy of the variables :p :D double length; //length of the box double breadth; //breadth of a box double height; //height of a box //member function declaration just like prototypes double getVolume(void); void setLength(double leng); void setBreadth(double bred); void setHeight(double heig); }; //Member functions definiition double Box::getVolume(void){ return length*breadth*height; } void Box::setLength(double leng) { length=leng; } void Box::setBreadth(double bred) { breadth=bred; } void Box::setHeight(double heig) { height=heig; } //main function for the program int main() { //define the basic variables Box box1; //declare box1 of type box Box box2; //declare box2 of type box double volume=0.0; //store the volume of a box here //box1 specification box1.setLength(6.0); box1.setBreadth(7.0); box1.setHeight(5.0); //box2 specification box2.setLength(12.0); box2.setBreadth(13.0); box2.setHeight(10.0); //volume of box1 volume=box1.getVolume(); cout<<"Volume of box1 = "<<volume<<endl; //volume of box2 volume=box2.getVolume(); cout<<"Volume of box2 = "<<volume<<endl; return 0; }
We use this process to avoid direct access to the members of the class
Another technique, we have used scope resolution operator :: to define the same function outside the class that means if we want to define class in outside from the class then we have to use scope resolution operator.
double Box::getVolume(void) //Box is the class name { return length*breadth*height; }
inside the class we define this like that
class Box{ public: double length; double height; double breadth; double getVolume(void){ return length*breadth*height; } };
and the member function is called just like this in a object from a class
Box box1 //create an object box1.getVolume(); //call member function for the object
C++ Access Modifiers:
Data hiding is one of the important features of Object Oriented Programming which allows preventing the functions of a program to access directly the internal representation of a class type .The access restriction to the class members is specified by the labeled public,private and protected sections with class body.The keywords public ,private and protected are called access specifiers.
A class can have multiple public,protected or private labeled sections.Each section remains in effect until either another section label or the closing right brace of the class body is seen.The default access for members and classes is private.
class Base{ public: //public members go here protected: //protected members go here; private: //private members go here; };
The public members:
A public meber is accessible from anywhere outside the class but within a program.You can set and get the value of public variables without any member function as shown in the following example:
#include<iostream> using namespace std; class Line { public: double length; void setLength(double len); double getLength(void); }; //Member functions definition double Line::getLength(void) { return length; } void Line::setLength(double len) { length=len; } //main function for the program int main() { Line line; //set the length line.setLength(6.0); cout<<"Length of the line: "<<line.getLength()<<endl; //set line length without member function line.length=10.0; cout<<"Length of line: "<<line.length<<endl; return 0; }
The private members:
A private member variable or function cannot be accessed or even viewed from outside the class.Only the class and friend functions can access private members.
By default all the members of a class would be private, for example in the following class width is a private member, which means until you label a member, it will be assumed a private member.
class Box { double width; public: double lengthl void setWidth(double wid); double getWidth(void); };
Practically we define data in private section and related functions in public section so that they can be called from outside of the class as shown in the following program.
#include<iostream> using namespace std; class Box{ public: double length; void setWidth( double wid ); double getWidth(void); private: double width; }; //member functions definitions double Box::getWidth(void) { return width; } void Box::setWidth( double wid ) { width=wid; } //main function int main() { Box box1; //set box length without member function box1.length=10.0; cout<<"Length of box: "<<box1.length<<endl; //box1.width=10.0; //it will give error because width is private box1.setWidth(20.0); //use member function to set it. cout<<"Width of the box: "<<box1.getWidth()<<endl; //use member function to get it. return 0; }
The protected members:
A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.
We will discuss derived classes and inheritance in next .But here I have derived one child class names SmallBox from a parent class Box
Following example is similar to above example and here width member will be accessible by any member function of its derived class SmallBox
#include<iostream> using namespace std; class Box { protected: double width; } class SmallBox:Box /SmallBox is the derived class. { public: void setSmallWidth(double wid); double getSmallWidth(void); }; //Member functions of child class double SmallBox::getSmallWidth(void) { return width; } void SmallBox::setSmallWidth(double wid) { width=wid; } //Main function for the program int main() { SmallBox box; box.setSmallWidth(5.0); cout<<"Width of box: "<<box.getSmallWidth()<<endl; return 0; }