Classes and Objects In CPP


Classes are the keyword to specifies the user-defined data type that acts as a blueprint for creating objects.
#include <iostream>
#include <vector>
using namespace std;
class Anand {
};
int main (){
return 0;
}
For defining or initializing the class the class Keyword should be there and starting Letter of name of that class should be an Uppercase letter and there is no involvement of parenthesis like method or functions declarations.
whenever we defining the variables and functions(methods) inside the class there are access specifiers comes into role .They control or allow to which part of your program can access the members(variables or functions ) inside the class.
Private : Private access specifiers are allowed to access only the members of the class inside the class. Outside the class, we can’t access the members(variables or functions ) of the class.If you don’t explicitly mention the access specifier by default it will be Private and outside the class you can’t use it.
Public : Public access specifiers are allowed to access the members of the class outside the class scope ,whenever we want to use it in the outside of class .
#include <iostream>
#include <vector>
using namespace std;
class Anand {
string Name;
string Evaluation;
//By default, the Name and Evaluation are set to private
public:
void CompanyEvaluation(string name,string evaluation){
Name = name;
Evaluation = evalution;
cout<<Name<<endl;
};
int main (){
Anand venture;
venture.CompanyEvaluation("AJ","200M $");
return 0;
}
Objects
For understanding objects, we can take an example of the SBI form of Account formation, where the form for account formation is actually a copy of the original form of account formation, where we defined some particular details to be filled by the user.
Similarly, Objects in C++ are like that it is a copy of a class, where with the help of objects we can use the structure or format of the class so that whenever we want to use a particular data type or structure defined in Class.we can use it by the help of objects.
In C++, an object is an instance of a class, which is used as a blueprint or template for creating objects.For declaring objects, it started with a LowerCase letter.
#include <iostream>
#include <vector>
using namespace std;
class Anand {
public:
string Name;
int price;
vector<string> Features;
void myObjectDetails(){
cout<<"Model"<<""<<Name<<endl<<"Price in Rupee"<<""<pric<<endl;
for(string Feature:Features){
cout<<" "<<Feature;
}
}
};
int main ()
{
Anand myObj;
myObj.Name = "MacBook Air",
myObj.price = 99999;
myObj.Features = {"16 GB Ram", "16 inch Display", "sky blue"};
myObj.myObjectDetails();
return 0;
}
I found a meme related to this topic, so I want to share which makes objects and classes more interesting, which absolutely makes it more relatable.
I really thankful to (Hitesh Choudhary ) Hitesh Choudhary sir for explaining this topic in a very light and interesting way in his C++ playlist on YouTube.
Subscribe to my newsletter
Read articles from Anand Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
