0% found this document useful (0 votes)
0 views

C++ Class Methods

The document explains the concept of classes and objects in C++, defining a class as a user-defined data type with methods and variables. It details how to define a class, declare objects, and implement methods both inside and outside the class definition. Examples of a rectangle class demonstrate how to calculate area and perimeter using member functions.

Uploaded by

heist994
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

C++ Class Methods

The document explains the concept of classes and objects in C++, defining a class as a user-defined data type with methods and variables. It details how to define a class, declare objects, and implement methods both inside and outside the class definition. Examples of a rectangle class demonstrate how to calculate area and perimeter using member functions.

Uploaded by

heist994
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.”

“A class is a collection of objects of the same kind.”


Defining Class:
A class is defined in C++ using the keyword class followed by the name of the class. The body
of the class is defined inside the curly brackets and terminated by a semicolon at the end.

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:

// C++ program for Inside Class Definition


#include <iostream>
using namespace std;

class rectangle
{
private:
int length, width, A, P;
public:

void d_in(int l, int w)


{
length = l;
width = w;
}

// area() function inside class


int area()
{
A=length * width;
return A;
}

// perimeter() function inside class


int perimeter()
{
P= 2 * (length + width);
return P;
}
};

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:

return_type Method_name(); // method outside class definition

};

// Outside the Class using scope resolution operator

return_type Class_name :: Method_name()


{

// body of member function

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++

// C++ program for Inside Class Definition


#include <iostream>
using namespace std;

class rectangle
{
private:
int length, width, A, P;

public:

void d_in(int l, int w)


{
length = l;
width = w;
}
int area();
int perimeter();

};
// area() function outside class

int rectangle :: area()


{
A=length * width;
return A;
}

// perimeter() function outside class


int rectangle :: perimeter()
{
P= 2 * (length + width);
return P;
}

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

You might also like