C++ Class Methods
C++ Class Methods
Class:
“Class is a fundamental block of a program that has its own set of methods and variables.
You can access these methods and variables by creating an object or the instance of the class”
“A Class is a user-defined data type that has data members and member functions.”
Syntax:
Object
“An Object is an instance of a Class. When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is allocated.”
Declaring Objects:
When a class is defined, only the specification for the object is defined; no memory or storage
is allocated. To use the data and access functions defined in the class, you need to create
objects.
Syntax:
ClassName ObjectName;
Data members:
Data members are the data variables and member functions are the functions used to
manipulate these variables together.
These data members and member functions define the properties and behavior of the objects in
a Class.
Class is a blueprint of an object, which has data members and member functions also known as
methods.
“A method is a procedure or function in the oops concept.”
“A method is a function that belongs to a class.”
“A function is a set of statements enclosed within curly brackets { } that take inputs, do the
computation, and provide the resultant output.”
There are two ways to define a procedure or function that belongs to a class:
Inside Class Definition
Outside Class Definition
1 Inside Class Definition
The member function is defined inside the class definition it can be defined directly.
Similar to accessing a data member in the class we can also access the public member functions
through the class object using the dot operator (.).
Syntax:
class class_name
{
public:
return_type Method_name() // method inside class definition
{
// body of member function
}
};
Example:
class rectangle
{
private:
int length, width, A, P;
public:
int main()
{
int a, b;
cout<<"Enter the length of Rectangle = ";
cin>>a;
cout<<"Enter the width of Rectangle = ";
cin>>b;
// Creating object
rectangle r;
r.d_in(a,b);
cout << "Perimeter: " << r.perimeter() << endl;
cout << "Area: " << r.area() << endl;
return 0;
}
Output:
Enter the length of Rectangle = 3
Enter the width of Rectangle = 4
Perimeter: 14
Area: 12
2 Outside Class Definition
The member function is defined outside the class definition it can be defined using the scope
resolution operator (::). Similar to accessing a data member in the class we can also access the
public member functions through the class object using the dot operator (.).
Syntax:
class Class_name
{
public:
};
Example: In the following code, a rectangle class, in which member function area() and
member function perimeter() is defined outside the class by using the scope resolution operator.
C++
class rectangle
{
private:
int length, width, A, P;
public:
};
// area() function outside class
int main()
{
int a, b;
cout<<"Enter the length of Rectangle = ";
cin>>a;
cout<<"Enter the width of Rectangle = ";
cin>>b;
// Creating object
rectangle r;
r.d_in(a,b);
cout << "perimeter: " << r.perimeter() << endl;
cout << "area: " << r.area() << endl;
return 0;
}
Output
Enter the length of Rectangle = 3
Enter the width of Rectangle = 4
Perimeter: 14
Area: 12