C++ Classes and Objects
What are Classes and Objects in C++ ?
Classes and objects are building blocks of C++. There are user-defined data types that are created to mimic real-world objects. Classes hold their own data members and member functions.
Its a collection of objects which have similar characteristics
Why Classes?
Programmers long demanded a feature in the programming language that allowed creating an interface with which they can define some common properties and methods that can be used by instances of that interface.
Let’s take an example to understand this –
Car as a class/interface, all cars in the world share some common properties –
- colour
- tyres
- mileage
- weight
- speed-limit etc
Similarly, all cars in world have common functions like – start(), brake(), stop(), reverse() etc.
With classes, we can create an interface of such common properties and methods and create thousands of instances i.e. objects to share them.
Whenever class is defined non memory is created. It is when objects of that class are created memory is allocated.
Another Example
All students in school have some common properties –
- Roll No
- DOB
- Weight
- Name
- Email ID
We can create a class with the name student to have all these properties defined.
If there are 1000 students in a school, we create 1000 objects unique to each student to store their details
Declaring Objects
Inside Main
int main(){ // declaring two objects here class_name object_name1, object_name2; }
Outside Main, just After Class definition
class class_name { //declare data members // declare member functions }object_name1, object_name2;
Example to Demonstrate above
#include <iostream> using namespace std; // name of the class class Student { // access specifier public: // data members variables int rollNo, weight, age; string name; // member functions void displayDetails() { cout << "Roll No: " << rollNo << endl; cout << "Name: " << name << endl; } }; int main(){ // declare Student object s1 Student s1; // assigning values s1.rollNo = 1; s1.weight = 80; s1.age = 21; s1.name = "Peter"; // calling function for s1 object s1.displayDetails(); return 0; }
Output
Roll No: 1
Name: Peter
- A Class is a user defined data-type which consists of data members and member functions.
- Data members are the variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class.
- In the above example of class Student, the data member will be rollNo, DOBand weight .
- Member Functions will be : displayDetails();
Defining Member Functions
Member functions can be declared in two ways –
- Inside the class (show in previous example)
- Outside the class (Using scope resolutor)
Following is the template if you want to declare member function outside class
return_type class_name::functionName(arguments){ // function working here }
Note, even though the function is defined outside, it must be declared inside the class as –
functionName(arguments);
Lets see an example below for both ways of function declaration –
Example of Function Declaration (Both ways)
#include using namespace std; // name of the class class Student { public: int rollNo, weight, age; string name; // function declaration & definition inside void printRollNo() { cout << "Roll No: " << rollNo << endl; } // function declaration inside void printName(); }; // function definition outside void Student::printName(){ cout << "Name: " << name << endl; } int main(){ Student s1; // assigning values s1.rollNo = 1; s1.name = "Peter"; s1.printRollNo(); s1.printName(); return 0; }
Output
Roll No: 1
Name: Peter
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment