Accessing Inherited Functions in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Accessing Inherited Functions in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Accessing Inherited Functions in C++  - Hackerrank Solution

Problem

You are given three classes A, B and C. All three classes implement their own version of func.

In class A, func multiplies the value passed as a parameter by 2 :

class A
{
    public:
        A(){
            callA = 0;
        }
    private:
        int callA;
        void inc(){
            callA++;
        }

    protected:
        void func(int & a)
        {
            a = a * 2;
            inc();
        }
    public:
        int getA(){
            return callA;
        }
};

in class B, func multiplies the value passed as a parameter by 3 :

class B
{
    public:
        B(){
            callB = 0;
        }
    private:
        int callB;
        void inc(){
            callB++;
        }
    protected:
        void func(int & a)
        {
            a = a * 3;
            inc();
        }
    public:
        int getB(){
            return callB;
        }
};

In class C, func multiplies the value passed as a parameter by 5 :

class C
{
    public:
        C(){
            callC = 0;
        }
    private:
        int callC;
        void inc(){
            callC++;
        }
    protected:
        void func(int & a)
        {
            a = a * 5;
            inc();
        }
    public:
        int getC(){
            return callC;
        }
};

You are given a class D:

class D 
{

	int val;
	public:
		//Initially val is 1
		 D()
		 {
		 	val = 1;
		 }


		 //Implement this function
		 void update_val(int new_val)
		 {

			
		 }
		 //For Checking Purpose
		 void check(int); //Do not delete this line.
};

You need to modify the class D and implement the function update_val which sets D’s val to new_val by manipulating the value by only calling the func defined in classes A, B and C.
It is guaranteed that new_val has only 2, 3 and 5 as its prime factors.


Input Format :

Implement class D’s function update_val. This function should update D’s val only by calling A, B and C’s func.

Constraints :

1<= new_val <= 10000
Note: The new_val only has 2, 3 and 5 as its prime factors.


Sample Output :

A’s func will be called once.
B’s func will be called once.
C’s func will be called once.

Explanation :

Initially, val = 1.
A’s func is called once:

val = val*2  
val = 2

B’s func is called once:

val = val*3
val = 6

C’s func is called once:

val = val*5
val = 30
Accessing Inherited Functions in C++ – Hacker Rank Solution
#include<iostream>

using namespace std;

class A
{
    public:
        A()
        {
            callA = 0;
        }
    private:
        int callA;
        void inc()
        {
            callA++;
        }

    protected:
        void func(int & a)
        {
            a = a * 2;
            inc();
        }
    public:
        int getA()
        {
            return callA;
        }
};

class B
{
    public:
        B()
        {
            callB = 0;
        }
    private:
        int callB;
        void inc()
        {
            callB++;
        }
    protected:
        void func(int & a)
        {
            a = a * 3;
            inc();
        }
    public:
        int getB(){
            return callB;
        }
};

class C
{
    public:
        C()
        {
            callC = 0;
        }
    private:
        int callC;
        void inc()
        {
            callC++;
        }
    protected:
        void func(int & a)
        {
            a = a * 5;
            inc();
        }
    public:
        int getC()
        {
            return callC;
        }
};

/* Accessing Inherited Functions in C++ - Hacker Rank Solution START */
class D : public A,B,C
{

	int val;
	public:
	//Initially val is 1
	D()
	{
	    val = 1;
	}
	//Implement this function
	void update_val(int new_val)
	{
             int a = new_val;
             while(new_val!=0)
             {
                 if(val==a)
                    break;
                 if(new_val%2==0)
                 {
                     A::func(val);
                     new_val/=2;
                 }
                 else if(new_val%3==0)
                 {
                     B::func(val);
                     new_val/=3;
                 }
                 else if(new_val%5==0)
                 {
                     C::func(val);
                     new_val/=5;
                 }
             }
			
	}
	//For Checking Purpose
	void check(int); //Do not delete this line.
};
/* Accessing Inherited Functions in C++ - Hacker Rank Solution END */

void D::check(int new_val)
{
    update_val(new_val);
    cout << "Value = " << val << endl << "A's func called " << getA() << " times " << endl << "B's func called " << getB() << " times" << endl << "C's func called " << getC() << " times" << endl;
}

int main()
{
    D d;
    int new_val;
    cin >> new_val;
    d.check(new_val);

}

23 thoughts on “Accessing Inherited Functions in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions”

  1. Hello there! Quick question that’s completely off topic. Do you know how to make your site
    mobile friendly? My weblog looks weird when browsing
    from my iphone 4. I’m trying to find a template or plugin that might
    be able to fix this issue. If you have any suggestions, please
    share. Many thanks!

    Reply
  2. Hiya, I am really glad I have found this information. Nowadays bloggers publish just about gossips and net and this is actually frustrating. A good website with exciting content, that is what I need. Thank you for keeping this web-site, I will be visiting it. Do you do newsletters? Cant find it.

    Reply
  3. I am really enjoying the theme/design of your blog. Do you ever run into any internet browser compatibility issues? A handful of my blog readers have complained about my blog not operating correctly in Explorer but looks great in Safari. Do you have any recommendations to help fix this issue?

    Reply
  4. Good day! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My website discusses a lot of the same subjects as yours and I believe we could greatly benefit from each other. If you are interested feel free to send me an email. I look forward to hearing from you! Great blog by the way!

    Reply
  5. Players that open a new account at either PokerStars NJ, PokerStars PA or PokerStars MI are welcomed with either play 1 hand, get $100 in bonus play, or a bonus match up to $600, not both. PokerStars launched its sports betting operation, BetStars, in New Jersey during 2018. The following year a monumental partnership with FOX Sports lead to a top-down rebrand from BetStars to FOX Bet. Pairing the worldwide brand recognition of FOX Sports with the decades of iGaming experience that The Stars Group brings to the table was a recipe for success. PokerStars Michigan also offers Zoom Poker Tournaments in Hold’em and Omaha games, including free games. In Zoom Poker, you won’t be playing against the same players each hand. Instead, you’ll be placed in a pool of players and you’ll be moved to a different table every time you fold. You can play Zoom Poker in cash games as well, but the Zoom Poker tournaments really keep the action moving.
    http://xn--hq1bq8p28dm5f.xn--mk1bu44c/bbs/board.php?bo_table=free&wr_id=65984
    Players who make an initial deposit at PokerStars will receive a 100% deposit-match bonus up to $600. The 100% deposit-match bonus from the PokerStars Casino promo code instantly doubles your first deposit. You’ll have access to your initial deposit PLUS a 100% match in bonus credits. Situs Slot Bonus 100 di awal terhitung sebagai web slot online tanpa deposit terhitung memiliki kumpulan promo bonus new member 100 yang tidak manfaatkan turnover. Slot bonus untuk member baru merupakan bonus yang tidak bakal memberi tambahan beban yang besar untuk anda. Dan cuma lewat agen khusus slot online bonus new member 100 di slot bonus, maka kamu bisa mendapatkan slot promo new member. Terkadang permasalahan dengan bonus new member 100 slot game bisa jadi dicoba, paling utama pada jam padat jadwal. Kamu dapat memakai slot bonus 100 di depan dalam permasalahan itu. Dalam Situs slot online Bonus new member 100 To kecil dapat melakukan deposit uang dari rekening bank di Mandiri, BNI, BCA serta Bank BRI, PULSA, OVO, GOPAY BRILink

    Reply
  6. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

    Reply

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock