Hackerrank Solution For Day 4: Class vs. Instance

Problem:- Write a Hackerrank Solution For Day 4: Class vs. Instance or Hacker Rank Solution Program In C++ For " Day 4: Class vs. Instance " or Hackerrank 30 days of code Java Solution: Day 4: Class vs. Instance or Hackerrank solution for 30 Days of Code Challenges.

Logic:- day 4 class and instance solution of hacker rank problem. Before explaining this problem we have to know that what is class and object(instance). Below is the syntax of class, object (instance) and methods (function). 

Class:- According to Wikipedia A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private, protected or public (by default access to members of a class is private). The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class Source:- Wikipedia

Explanation:- Below is an explanation of class This example is taken from hacker rank read full article click here

class Dog{ // class name
    static String unnamed = "I need a name!"; // class variable
    int weight; // instance variable
    String name; // instance variable
    String coatColor; // instance variable
    Dog(){ // default constructor
        this.weight = 0;
        this.name = unnamed;
        this.coatColor = "none";
    Dog(int weight, String color){ // parameterized constructor
        // initialize instance variables
        this.weight = weight; // assign parameter's value to instance variable
        this.name = unnamed; 
        this.coatColor = color; 
    Dog(String dogName, String color){ // overloaded parameterized constructor
        // initialize instance variables
        this.weight = 0;
        this.name = dogName; 
        this.coatColor = color; 

If you want to 30 days solution (All previous solution from day 0 ) from Day 0 please check the below link. You can also find more program in below of this post. 

Tip:- Before solving this (Day 4: Class vs. Instance) problem make sure you completed all below problem, if not check the below solution with the Full explanation.

Copy the colored code and paste it into hacker rank editor. If you have any query feel free to share with me, and if you like my work please share this Article.

Submit Your Solution Here:- Click Here


usinHere:space std;
#include <iostream>

class Person{
        int age;
        Person(int initialAge);
        void amIOld();
        void yearPasses();

    Person::Person(int initialAge){
        // Add some more code to run some checks on initialAge
            cout<<"Age is not valid, setting age to 0."<<endl;


    void Person::amIOld(){
        // Do some computations in here and print out the correct statement to the console 
            cout<<"You are young."<<endl;
        else if(age>=13 && age<=17)
            cout<<"You are a teenager."<<endl ;
            cout<<"You are old."<<endl;

    void Person::yearPasses(){
        // Increment the age of the person in here

int main(){
    int t;
int age;
    cin >> t;
    for(int i=0; i < t; i++) {
    cin >> age;
        Person p(age);
        for(int j=0; j < 3; j++) {
cout << '\n';

    return 0;


Hackerrank Solution For Day 4: Class vs. Instance output

You May Also Like:-

No comments:

Post a Comment