Class is like a skeleton or blueprint for an object. It is a user defined data type, and many instances of the same class can be created containing copies of its members, called an object.
A class contains data members and member functions. For example, for Student:
No storage is assigned for a class until an object is created.
class ClassName
{
accessSpecifier: //either public, private or protected
ClassName() //Constructor
{
//initializations
}
memberFunction1(){}
};
Remember that Class definitions go on top of your main function! :D (if not in their own header file)
Objects are instances of a class, and a special class function called Constructor performs the initialization of an object of a class. Similarly, a Destructor is used to destroy an Object. Constructors will have the same name as the Class Name, and no return value.
When an Object is created, it is initialized with the values that are declared from the Constructor function inside the class. If no Constructor is defined, the compiler calls the Default Constructor which initializes the object randomly.
Parametrized Constructors let you provide the initialization value:
class Cylinder
{
public: // access specifier
int height, radius;
Cylinder(int h,int r) //Constructor
{
height = h;
radius = r;
}
};
int main()
{
Cylinder cylinderA(12,3);
Cylinder cylinderB(6, 2);
cout << "Cylinder A Radius Value: " << cylinderA.radius;
}
---------------------------------------------------
Cylinder A Radius Value: 3
[Finished in 0.38s]
Another Example: (Without Constructor)
class Car{
public:
string brand, model;
int horsepower;
};
int main(){
Car myFirstCar;
myFirstCar.brand = "Suzuki";
myFirstCar.model = "Swift";
myFirstCar.horsepower = 110;
cout << myFirstCar.brand << " " << myFirstCar.model << " has a horsepower of " << myFirstCar.horsepower << endl;
return 0;
} //end main
---------------------------------------------------------
Suzuki Swift has a horsepower of 110
[Finished in 0.39s]
Same Example with Constructor Function:
class Car{
public:
string brand, model;
int horsepower;
// constructor
Car(string b, string m, int hp){
// initialize private variables
brand = b;
model = m;
horsepower = hp;
}
};
int main(){
Car myCurrentCar("Hyundai", "Ioniq", 139);
cout << myCurrentCar.brand << " " << myCurrentCar.model << " has a horsepower of " << myCurrentCar.horsepower << endl;
return 0;
} //end main
Note how the initialization for the object myCurrentCar is different compared to myFirstCar and you can directly pass the parameters to it.
You can duplicate objects using a Copy Constructor, like this:
class Car{
public:
string brand, model;
int horsepower;
// constructor
Car(string b, string m, int hp){
// initialize private variables
brand = b;
model = m;
horsepower = hp;
}
//copy constructor
Car(Car &obj){ //pass a Car object to the constructor
brand = obj.brand;
model = obj.model;
horsepower = obj.horsepower;
}
};
int main(){
// with a constructor
Car myCurrentCar("Hyundai", "Ioniq", 139);
cout << myCurrentCar.brand << " " << myCurrentCar.model << " has a horsepower of " << myCurrentCar.horsepower << endl;
// copy constructor
Car myCurrentCarCopy = myCurrentCar;
cout << myCurrentCarCopy.brand << " " << myCurrentCarCopy.model << " has a horsepower of " << myCurrentCarCopy.horsepower << endl;
return 0;
}
------------------------------------------
Hyundai Ioniq has a horsepower of 139
Hyundai Ioniq has a horsepower of 139
[Finished in 0.35s]
<aside> 💡 c++ hackerrank Classes
Classes in C++ are user defined types declared with keyword class that has data and functions . Although classes and structures have the same type of functionality, there are some basic differences. The data members of a class are private by default and the members of a structure are public by default. Along with storing multiple data in a common block, it also assigns some functions (known as methods) to manipulate/access them. It serves as the building block of Object Oriented Programming.
It also has access specifiers, which restrict the access of member elements. The primarily used ones are the following:
public: Public members (variables, methods) can be accessed from anywhere the code is visible.
private: Private members can be accessed only by other member functions, and it can not be accessed outside of class.
Class can be represented in the form of
class ClassName { access_specifier1: type1 val1; type2 val2; ret_type1 method1(type_arg1 arg1, type_arg2 arg2,...) ... access_specifier2: type3 val3; type4 val4; ret_type2 method2(type_arg3 arg3, type_arg4 arg4,...) ... };
It's a common practice to make all variables private, and set/get them using public methods. For example:
class SampleClass { private: int val; public: void set(int a) { val = a; } int get() { return val; } };
We can store details related to a student in a class consisting of his age (int), first_name (string), last_name (string) and standard (int).
You have to create a class, named Student, representing the student's details, as mentioned above, and store the data of a student. Create setter and getter functions for each element; that is, the class should at least have following functions:
get_age, set_age
get_first_name, set_first_name
get_last_name, set_last_name
get_standard, set_standard
Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,). You can refer to stringstream for this.
Input Format
Input will consist of four lines. The first line will contain an integer, representing the age. The second line will contain a string, consisting of lower-case Latin characters ('a'-'z'), representing the first_name of a student. The third line will contain another string, consisting of lower-case Latin characters ('a'-'z'), representing the last_name of a student. The fourth line will contain an integer, representing the standard of student.
Note: The number of characters in first_name and last_name will not exceed 50.
Output Format
The code provided by HackerRank will use your class members to set and then get the elements of the Student class.
Sample Input
15 john carmack 10
Sample Output
15 carmack, john 10
15,john,carmack,10
</aside>