C++ Class Representations
Here we are going to make our own C++ program to make our own classes. So what is a class? Classes are like building blocks in C++. They are user-defined datatype and they hold their own data and functions which can be accessed by the user by creating an object of the class. Class leads to OOPS (object-oriented programming in C++).
For example, let’s consider a class SmartPhone
. There are many phones with different brands and names but all phones have somethings in common like processor, RAM, storage, etc. So here, processor, RAM, storage becomes data type of the class and a function that displays it to the user becomes the member function of the class.
When a class is defined, no memory is allocated to it but when an object of the class is created then memory is allocated for that object of the defined class.
In class, there are types of access specifiers like public, private, and protected.
- Public – this access specifier gives full access to the data and function of the class to the user.
- Private – this access specifier contains private data of the class that is not accessible to the user example there is a Smartphone class as mentioned above and there is a feature that can affect the privacy or security of the user so the company can make it as private so no can have access to it.
- Protected – this access specifier gives access to the data and functions to any subclass of the class.
C++ program of a class
#include <bits/stdc++.h> using namespace std; class SmartPhone { // private access specifier of the class company private: string confidentialInfo; // public Access specifier of the class company public: // Data Member of the class company string phoneName; string RAM; string storage; string processor; // Member Function() of the class company void printPhoneName() { cout << "phone name is : " << phoneName<<endl; } void printProductDetails(){ cout<<endl<<"Its specs are -:"<<endl<<"processor: " <<processor<<endl<<"storage : "<<storage <<endl<<"RAM : "<<RAM; } }; int main() { // creating object of the class company SmartPhone c; // accessing data from the class company c.phoneName = "Oneplus 6"; c.processor = "Snapdragon 845"; c.storage = "128gb"; c.RAM = "8gb"; // accessing function from the class company c.printPhoneName(); c.printProductDetails(); return 0; }
Function implementation
- printPhoneName
this function prints the name of the smartphone.
printProductDetails
This function prints the specification of the smartphone.
Implementation
First, we have defined a class SmartPhone in this class there are 2 specifiers public and private. In private specifier, we have created a string confidentialInfo which is not accessible to the user. In the public specifier, we have created data types and functions which have full access to the user. In the main function, we have created an object of the class, then we have inserted data in the data types and called the function to get the phone name and its specification.
Output
phone name is: Oneplus 6 Its specs are -: processor: Snapdragon 845 storage : 128gb RAM : 8gb
Leave a Reply