C++ language

adplus-dvertising
Basic Classes
Previous Home Next

Class is a template definition of methods and variables in a particular kind of object . Thus, an object is a specific instance of a class; it contains real values instead of variables.

Class is use for bind data and its operated function together.

For external use its necessary to hidden the data and function. Or Class is the container of object.

Class Specification have two part::
  1. Class Declaration
  2. Class Function definitions
Class Declaration:
class class_name
{
private:
variable declarations;
    function declarations;
public:
variable declarations;
    function declarations;
};

The keyword class specified that what follow in an abstract data of type class_name. private and public are access modifier.

where private is visible with is the class where public data member is can be access with in the class and also outside the class.

class class_name
{
private:
int num1;
float num2;
public:
void getdata(int num1,float num2);
void putdata(void);
};

Create Objects

  • Object is real time entity. Object have some attribute and also some feature.
  • Object can created with class name as-
class_name object_name;
And also created as:
class class_name
{
private:
int num1;
float num2;
public:
void getdata(int num1,float num2);
void putdata(void);
}
c1,c2,c3;            

Class Function Definition

Member function can be defined in two ways:
  1. Out Site the class definition
  2. Inside the class definition
Outsite the class definition
return_type class_name:: function_name(argument declare)
{
             function body;
}
Inside the class definition
class class_name
{
private:
int num1;
	float num2;
public:
		void getdata(
		int num1,float num2)       
		  // inside class declaration
	{
cin>>num1;
cin>>num2;
	}
		void putdata(void);    
		// inside class declaration
{
cout<<num1;
cout<<num2;
}
}
c1,c2,c3;                    //object created.
Previous Home Next