Professional Documents
Culture Documents
A class in C++ is the building block, that leads to Object-Oriented programming. It is a user-
defined data type, which holds its own data members and member functions, which can be
accessed and used by creating an instance of that class. A C++ class is like a blueprint for
an object.
For Example: Consider the Class of Cars. There may be many cars with different names
and brand but all of them will share some common properties like all of them will have 4
wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits,
mileage are their properties.
A Class is a user defined data-type which has data members and member functions.
Data members are the data variables and member functions are the functions used to
manipulate these variables and together these data members and member functions defines
the properties and behaviour of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage etc and
member functions can be apply brakes, increase speed etc.
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.
A class is defined in C++ using keyword class followed by the name of class. The body of
class is defined inside the curly brackets and terminated by a semicolon at the end.
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;
Accessing data members and member functions: The data members and member functions
of class can be accessed using the dot(‘.’) operator with the object. For example if the name
of object is obj and you want to access the member function with the name printName() then
you will have to write obj.printName() .
Accessing Data Members
The public data members are also accessed in the same way given however the private
data members are not allowed to be accessed directly by the object. Accessing a data
member depends solely on the access control of that data member.
There are three access modifiers : public, private and protected.
#include <iostream>
using namespace std;
class A
{
// Access specifier
public:
// Data Members
int roll_number;
// Member Functions()
void display()
{
cout << "Roll number is: " << roll_number;
}
};
int main() {
OUTPUT
Roll number is: 1001
#include <iostream>
using namespace std;
class A
{
// Access specifier
public:
// Data Members
string name;
// Member Functions()
void display()
{
cout << "Student name is: " << name;
}
};
int main() {
OUTPUT
Student name is: John
To define a member function outside the class definition we have to use the scope
resolution :: operator along with class name and function name.
#include <iostream>
using namespace std;
class A
{
// Access specifier
public:
// Data Members
string name;
int roll_number;
// Member Functions()
void display()
{
cout << "Student name is: " << name<<endl;
}
void print();
};
void A::print()
{
cout << "Roll number is: " << roll_number;
}
int main() {
return 0;
}
Output:
Student name is: John
Roll number is: 1001
Note that all the member functions defined inside the class definition are by default inline,
but you can also make any non-class function inline by using keyword inline with them.
Inline functions are actual functions, which are copied everywhere during compilation, like
pre-processor macro, so the overhead of function calling is reduced.
The above style is considered as a bad programming style. The best programming style is
to just write the prototype of function inside the class and specify it as an inline in the
function definition.
For example:
class S
{
public:
int square(int s); // declare the function
};
#include <iostream>
using namespace std;
class operation
{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void operation :: get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";
cin >> b;
}
int main()
{
cout << "Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}
OUTPUT
Enter first value: 45
Enter second value: 15
Addition of two numbers: 60
Difference of two numbers: 30
Product of two numbers: 675
Division of two numbers: 3
Syntax:
class_name array_name [size] ;
class A
{
int x;
public:
void setX(int i) { x = i; }
int getX() { return x; }
};
int main()
{
A z[4];
int i;
return 0;
}
Output:
z[0].getX(): 0
z[1].getX(): 1
z[2].getX(): 2
z[3].getX(): 3
How to initialize Array of objects with parameterized constructors in C++
#include <iostream>
using namespace std;
class Test {
// private variables.
private:
int x, y;
public:
// parameterized constructor
Test(int cx, int cy)
{
x = cx;
y = cy;
}
// method to add two numbers
void add() { cout << x + y << endl; }
};
int main()
{
// Initializing 3 array Objects with function calls of
// parameterized constructor as elements of that array
Test obj[] = { Test(1, 1), Test(2, 2), Test(3, 3) };
return 0;
}
Whenever an object of a class is passed to a member function of the same class, its data
members can be accessed inside the function using the object name and the dot operator.
However, the data members of the calling object can be directly accessed inside the
function without using the object name and the dot operator.
To understand how objects are passed and accessed within a member function, consider
this example.
A program to demonstrate passing objects by value to a member function of the same class
#include <iostream>
using namespace std;
class weight
{
int kilogram;
int gram;
public:
void getdata ();
void putdata ();
void sum_weight (weight,weight) ;
};
int main ()
{
weight w1,w2,w3;
cout<<"Enter weight in kilograms and grams\n";
cout<<"\n Enter weight #1" ;
w1.getdata();
cout<<" \n Enter weight #2" ;
w2.getdata();
w3.sum_weight(w1,w2);
cout<<"/n Weight #1 = ";
w1.putdata();
cout<<"Weight #2 = ";
w2.putdata();
cout<<"Total Weight = ";
w3.putdata();
return 0;
}
OUTPUT:
Enter weight in kilograms and grams
Enter weight #1
Kilograms: 12
Grams: 560
Enter weight #2
Kilograms: 24
Grams: 850
To understand the concept of returning an object by value from a function, consider this
example.
Below example is a program to demonstrate the concept of returning objects from a function
#include <iostream>
using namespace std;
class weight
{
int kilogram;
int gram;
public:
void getdata ();
void putdata ();
weight sum_weight (weight) ;
} ;
int main ()
{
weight w1,w2,w3;
cout<<"Enter weight in kilograms and grams\n";
cout<<"\n Enter weight #1" ;
w1.getdata();
cout<<" \n Enter weight #2" ;
w2.getdata();
w3 = w1.sum_weight (w2);
As the return type of function is weight (an object of class weight), a temporary object temp
is created within the function for holding return values. These values are accessed as temp
kilogram and temp gram by the function. When the control returns to main (), the object
temp is assigned to the object w3 in main( ).
The below figure represents accessing of member variables within the function and
returning a result from the temporary object.