Classes and Objects



Class

  • Class is created using a keyword class
  • It holds data and functions.
  • Class link Code and Date.
  • The Data and Function of the class are called Member of the Class.


Eg:
class class-name(

public/private/protected:(by default it is private)

Data Members

Member Functions

);


Object

  • Objects are Variables.
  • They are copy of the Class
  • Each of them has Property and Behaviour
  • Property is defined through Data Element
  • Behaviour is defined through Member Functions called Methods



#include <iostream> //header file
using namespace std;

//creating a class, here data and Function are combine together in a class. Class is single unit, in which data and function using as group, this mechanism called Encapsulation.  


class square{

                                //Here “int x” is private data is hidden, it cannot be accessed by outside the Class,                                      this mechanism is called Abstraction. We can see the interface but the implement                                      is hidden. 

int x;
public:
int area(int);
};
 
int square::area(int a){
x=a;
return x*x*x;
}
int main()
{
square sqr;
cout<<sqr.area(4);
return 0;
}


0 Comments