-3

Okay I'm not sure how to explain this but here goes. I want to get name (from return name) for both the Dog and Cat classes into int main so that they print out where fido.name and spot.name are. How would I do this?

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class Dog {

   private:
      // constructor
      Dog(string name) {

         this->name = name;
         name = "Fido";
         cout << "Dog's name is " << name << endl;
      }
   public:
      static string name;
      static string GetName();

};

string Dog::GetName(){
   return name;
}

class Cat {

   private :
      // constructor
      Cat(string name) {

         this->name = name;
         name = "Fido";
         cout << "Cat's name is " << name << endl;
      }
   public :
      static string name;
      static string GetName();
};

string Cat::GetName(){
   return name;
}

int main() {

   Dog fido("Fido"); //error here stating that Dog::Dog(std::string name)
   //declared at line 13 is inaccessible 
   Cat spot("Spot");

   cout << "From main, the Dog's name is " << fido.name << endl;
   cout << "From main, the Cat's name is " << spot.name << endl;

   cout << "Hit any key to continue" << endl;

   system("pause");

   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    You might want to read the [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), because the problems you have are extremely basic. – Some programmer dude Mar 26 '15 at 21:25

2 Answers2

1

You will have to make the constructor public (with tag 'public:') or you will not be able to create the objects from outside the class.

And also, remove all 'static' keyworkds, because if you declare it as static, you will not be able to have more than 1 different "Cats" and "Dogs"

I hope it helps

lui
  • 47
  • 3
  • Thanks for the input. I'm not allowed to make the constructor public unfortunately, is there some way I could call in `name` from the public area of the two classes instead of using `fido.name` and `spot.name`? – Paul Merimee Mar 26 '15 at 21:33
  • Ok, then, as Dog and Cat have its name "static", you must not create the object Dog and Cat (valid for case but this is kind of weird). So remove creations, and return to your static keywords because will need these. You can do for example Dog.name = "Fido" And after that Dog.GetName() will provide you the correct name Is that a programming exercise? – lui Mar 26 '15 at 21:37
  • And no, you cannot change the name other way than accessing name attribute directly, unless you can use "friend" keyword :) Another nasty hack – lui Mar 26 '15 at 21:44
  • I tried that already but I guess I'm not allowed to use it :( Where would I put Dog.name = "Fido" and the Dog.GetName()? – Paul Merimee Mar 26 '15 at 21:49
  • Instead of your line Dog fido("Fido"); //error here stating that Dog::Dog(std::string name) simply put Dog.name = "Fido". After that, you cout is perfectly legal, you could also use Dog.GetName() but, as GetName will hold a copy of "name", both calls will work. Obviosuly the same solution for your cats :) – lui Mar 26 '15 at 21:53
  • Okay so I figured out what I think will work. If I put simply GetName where fido and spot will be it should work. Except I can't bring GetName into int main. Would you happen to know how to do this? – Paul Merimee Mar 26 '15 at 22:30
-1

Use the GetName() functions.

cout << "From main, the Dog's name is " << fido.GetName() << endl;
cout << "From main, the Cat's name is " << spot.GetName() << endl;

You'll have to move the constructor the public sections of the classes for

Dog fido("Fido");
Cat spot("Spot");

to work.

As I looked more into your class, I realized you've got more errors. name needs to non-static member variable and GetName() needs to be non-static member function -- in both classes.

Dog needs to be something like:

class Dog {

   public:

      Dog(string name) {

         this->name = name;
      }

      string GetName() const;

   private:

      static string name;

};

You'll have to make similar changes to Cat.

R Sahu
  • 204,454
  • 14
  • 159
  • 270