Saturday, 25 March 2017

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.

Hackerrank Solution For Day 4: Class vs. Instance

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.

1. Hackerrank Solution For Day 0: Hello, World.

2. Hackerrank Solution For Day 1: Data Types. 

3. Hackerrank Solution For Day 2: Operators.

4. Hackerrank Solution For Day 3: Intro to Conditional Statements 

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


Solution:-

usinHere:space std;
#include <iostream>

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

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

    }

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

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

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

    return 0;
}

Output:-


Hackerrank Solution For Day 4: Class vs. Instance

You May Also Like:-








8. Hackerrank Solution For Day 12: Inheritance

9. Hackerrank Solution For Day 13: Abstract Classes

10. Hackerrank Solution For Day 14: Scope

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment