Hey there, welcome to Blog # 6 of the oop series in c++. In the last part, we discussed the UML and different types of relations in classes/objects in C++. As you guys know quite a lot now, I feel that it is time to move on to objects and classes.
Introduction:
Object-Oriented Programming is associated with objects and classes. In object-oriented programming, a class is a blueprint for creating objects, providing initial values for state (member variables or attributes), and implementing details of the behavior of that object (member functions or methods). An object in OOPS is a self-contained component that consists of methods and properties to make a particular type of data useful. When the individual objects are created, they inherit all the variables and functions from the class.
Uses:
- The real-world problem becomes easy to understand from the perspective of programming.
- Interactions can be easily modeled.
Syntax:
class className {
Data_type Member_variable;
Return_type Member_function();
};
Example:
class Student{
public:
int roll_num;
float CGPA;
void set_data();
};
Explanation:
- ✔ class keyword is used to create a class.
- ✔ Then class name “Student” is mentioned.
- ✔ Public is an access specifier that we will talk about later.
- ✔ At the closing of the class body, use a semi-colon.
Why we use member functions:
- Methods provide the interface that other classes use to access and modify the properties of an object.
- They model the behaviors of an object.
Accessing member functions:
Member variables and methods are accessed in the same way. Members of an object can be accessed using
- dot operator (.) to access via the variable name
- arrow operator (->) to access via a pointer to an object
Declaration Of Member Function:
There are two ways to declare the functions of a class:
- Inside a class definition
- Outside class definition
Inside Class:
Example:
class Student{
public:
int roll_name;
float CGPA;
void set_data(){
roll_num = 0;
CGPA = 0;
}
};
Outside Class:
Example:
class Student{
public:
int roll_name;
float CGPA;
void set_data();
};
void Student:: set_data(){
roll_num = 0;
CGPA = 0;
}
Explanation:
If you want to define a function outside the class, then it must first have declared inside the class. This is done by specifying the name of the class, followed by the scope resolution:: operator, followed by the name of the function.
Parameterized Methods:
Parameterized methods accept parameters.
class Student{
public:
int roll_name;
float CGPA;
void set_data(int r, float gpa);
};
void Student:: set_data(int r, float gpa){
roll_num = r;
CGPA = gpa;
}
Create An Object:
In order to declare an object in the main function, we first write the class name and then the object name as we do in the declaration of a variable( int a ).
#include <iostream>
using namespace std;
class Student{
public:
int roll_name;
float CGPA;
void set_data(int r, float gpa);
};
void Student:: set_data(int r, float gpa){
roll_num = r;
CGPA = gpa;
}
int main(){
Student s1; // object declaration
return 0;
}
Conclusion:
Okay, Guys, that's all about objects and classes.🙌 Now, it's time to create some classes and initialize objects.✌ In the next part, we will discuss access specifiers and constructors. So stay tuned and start to do some programming on your own.😉