Introduction:
In this article, we are going to learn about the classes and objects.
C++ is an object-oriented programming language. C++ is associated with classes and objects along with its attributes and methods.
The following are the features of the classes and objects in C++.
Data hiding: It can set permissions to restrict the access of the data.
Code Reusability: we can reduce redundancy by using reusable code with the help of inheritance.
Flexibility: we can use the class in many forms using the concept of polymorphism.
Class:
A class in C++ is the building block that leads to Object-Oriented programming. class is a user-defined data type. It holds its own data member and data functions. A Class is like a blueprint for an object.
These are the data members and member functions. It defines the properties and behavior of the objects in a Class. Classes further implement the core concepts like encapsulation, data hiding, and abstraction.
How to Create a Class?
We use the “class” keyword to create a class.
class Bio { // The class
public: // Access specifier
int age; // Attribute (int variable)
}; // class name ends with a semicolon
Explanation:
The class keyword is used to create a class called Bio.
The public keyword is an access specifier, which specifies that members (attributes and methods) of a class are accessible from outside the class.
Inside the class we declared an integer variable called age. this variable is known as an attribute.
Class Methods:
There are two ways to define the member functions of a class.
Inside class definition
Outside class definition
1. Inside Class Definition:
It is preferred for small functions. it provides the arguments in the function header. The following example defines a member function inside a class.
Example:
#include <iostream>
using namespace std;
// define a class
class myone
{
public:
// inside class definition of the function
void sum(int num1, int num2) // function header
{
cout << "The sum of the numbers is: "; // function body
cout << (num1 + num2) << "\n\n";
}
};
int main()
{
// create an object of the class
myone obj;
// call the member function
obj.sum(30, 2);
return 0;
}
Output:
The sum of the numbers is: 32
2. Outside class definition:
Here, we use the scope resolution operator (::) to define a member function outside its class. it still needs to be declared inside a class.
Example:
#include <iostream>
using namespace std;
// define a class
class myone
{
public:
// declare the member function
void sum(int num1, int num2);
}; // outside class definition of the function
void my_class::sum(int num1, int num2) // function header
{
cout << "The sum of the numbers is: "; // function body
cout << (num1 + num2) << "\n\n";
}
int main()
{
// create an object of the class
my_class obj;
// call the member function
obj.sum(30, 2);
return 0;
}
Output:
The sum of the numbers is: 32
Object:
An Object is an instance of a Class. objects have states and behavior. we can also create multiple objects in one class. When a class is defined no memory is allocated but when it is instantiated that means the object is created then the memory is allocated. To access the functions and to use the data we defined in the class, we use the objects.
syntax:
ClassName ObjectName;
How to create an object?
To create an object of Bio, specify the class name.
#include <iostream>
#include <string>
using namespace std;
class Bio { // The class
public: // Access specifier
int age; // Attribute (int variable)
};
int main() {
Bio Obj; // Create an object of Bio
// Access attributes and set values
Obj.age = 15;
// Print values
cout << Obj.age<< "\n";
return 0;
}
Output:
15
Explanation:
Firstly, we have to create the class.
The public keyword is an access specifier.
Now in the main function, we have to create an object of Bio.
By use of an object, we have to access the attributes and set the values.
Print the values.
Multiple objects
we can also create multiple objects of a single class. because the objects have different attribute/property values.
#include <iostream>
#include <string>
using namespace std;
class drinks {
public:
string batchno;
string drinkname;
int priceinrupees;
float litres;
};
int main() {
drinks drinkObj1;
drinkObj1.batchno = "8564";
drinkObj1.drinkname = "thumsup";
drinkObj1.priceinrupees = 90 ;
drinkObj1.litres = 2.5;
drinks drinkObj2;
drinkObj2.batchno = "7864";
drinkObj2.drinkname = "sprite";
drinkObj2.priceinrupees = 90 ;
drinkObj2.litres = 2.5;
cout << drinkObj1.batchno << " " << drinkObj1.drinkname << " " << drinkObj1.priceinrupees << " "<< drinkObj1.litres << "\n";
cout << drinkObj2.batchno<< " " << drinkObj2.drinkname << " " << drinkObj2.priceinrupees << " " << drinkObj2.litres << "\n";
return 0;
}
Output:
8564 thumsup 90 2.5
7864 sprite 90 2.5