hi i am trying to "divide" the following code...so that it has a header file and the main file. i tried to seperate the it but when i do it says "nothing to do at all" or "nothing happened at all"(one of them)
ere is the original code. can you pls seperate it into one that a "dog.h" and "dog.cpp"
p.s. i'll appreciate it if you could do this as fast as you can cuz i have a test that i'm supposed to do the same to a similar code.

#include <iostream>
using namespace std;
class Dog {
private:
    int age;
    int weight;
public:
    Dog();牋牋牋//Constructor
    ~Dog();牋牋//Destructor
    void setAge(int age);
    int getAge();
    void setWeight(int weight);
    int getWeight();
    void speak();
};
Dog::Dog()
{
    age = 0;
    weight = 0;
    cout << "Dog Constructor Called" << endl;
}
Dog::~Dog()
{
    cout << "Dog Destructor Called" << endl;
}
void Dog::setAge(int age)
{
    this->age = age;
}
int Dog::getAge()
{
    return age;
}
void Dog::setWeight(int weight)
{
    this->weight = weight;
}
int Dog::getWeight()
{
    return weight;
}
void Dog::speak()
{
cout << "BARK!!" << endl;
}
int main()
{
    Dog fido;
    Dog rover;
    cout << "Rover is " << rover.getAge() << " years old." << endl;
    cout << "He weighs " << rover.getWeight() << " lbs." << endl;
    cout << endl;
    cout << "Updating Rover's Age and Weight" << endl;
    rover.setAge(1);
    rover.setWeight(10);
    cout << "Rover is " << rover.getAge() << " years old." << endl;
    cout << "He weighs " << rover.getWeight() << " lbs." << endl;
    cout << endl;
    cout << "Fido is " << fido.getAge() << " years old." << endl;
    cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    cout << "Setting Fido to be the same as Rover" << endl;
    fido = rover;
    cout << "Fido is " << fido.getAge() << " years old." << endl;
    cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    rover.speak();
    fido.speak();
    return 0;
}

Dani AI

Generated

Practical checklist and troubleshooting notes that build on ’s split and ’s comment about using cin.

Most likely causes for the “nothing to do” / “nothing happened” messages or a silent console are: the new .cpp file wasn’t added to the project (so the compiler never compiles the method definitions), the files weren’t saved with the correct extensions (.cpp/.h), or the IDE thinks the target is up-to-date. In Dev-C++ create a Console Project, save all files into the project folder, add both main.cpp and dog.cpp to the project manager, then use Build → Rebuild All (or Clean + Build) so timestamps and object files are refreshed.

Good file layout and small best-practices (no duplicate definitions, no namespace leakage):

  • dog.h: declarations only, with an include guard; avoid putting using namespace std; in headers.
  • dog.cpp: #include "dog.h" plus any standard headers needed for method bodies (e.g., <iostream>), then the method definitions with scope resolution (Dog::...).
  • Never #include "dog.cpp" from main — that causes duplicate definitions if the .cpp is also compiled separately.

Command-line compile example (shows the requirement to compile both translation units):

g++ main.cpp dog.cpp -o dogProgram

About the “black screen / press any key” behaviour: that is the console pausing after the program ends. Running the generated .exe from a Windows command prompt (cmd) will show the full output and any errors. For a portable pause (instead of system("pause")) place a simple input-wait after all output, for example:

std::cout << "Press ENTER to exit...";
std::cin.get();

(if earlier input was used, flush the leftover newline with std::cin.ignore() before get()).

Final quick checks: file extensions, header guards, ensure only declarations go into headers (or mark small functions inline), add all .cpp files to the project, and use Rebuild All if the IDE reports “nothing to do.” The assignment fido = rover; is fine here since the class only has built-in members.

Recommended Answers

All 10 Replies

Quickie:

class Dog {
private:
    int age;
    int weight;
public:
    Dog();牋牋牋//Constructor
    ~Dog();牋牋//Destructor
    void setAge(int age);
    int getAge();
    void setWeight(int weight);
    int getWeight();
    void speak();
};
#include <iostream>
using namespace std;
Dog::Dog()
{
    age = 0;
    weight = 0;
    cout << "Dog Constructor Called" << endl;
}
Dog::~Dog()
{
    cout << "Dog Destructor Called" << endl;
}
void Dog::setAge(int age)
{
    this->age = age;
}
int Dog::getAge()
{
    return age;
}
void Dog::setWeight(int weight)
{
    this->weight = weight;
}
int Dog::getWeight()
{
    return weight;
}
void Dog::speak()
{
cout << "BARK!!" << endl;
}
int main()
{
    Dog fido;
    Dog rover;
    cout << "Rover is " << rover.getAge() << " years old." << endl;
    cout << "He weighs " << rover.getWeight() << " lbs." << endl;
    cout << endl;
    cout << "Updating Rover's Age and Weight" << endl;
    rover.setAge(1);
    rover.setWeight(10);
    cout << "Rover is " << rover.getAge() << " years old." << endl;
    cout << "He weighs " << rover.getWeight() << " lbs." << endl;
    cout << endl;
    cout << "Fido is " << fido.getAge() << " years old." << endl;
    cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    cout << "Setting Fido to be the same as Rover" << endl;
    fido = rover;
    cout << "Fido is " << fido.getAge() << " years old." << endl;
    cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    rover.speak();
    fido.speak();
    return 0;
}

It is, of course, missing an #include in the cpp, but I'm being understandably lazy.

thanks! wierd hing is i did the exact same thing earlier but it gave me that error thingy. ok small thing the code u wrote works (even with the #include dog.h but it just gives me a black screen and "press any key to continue". any ideas?
thanks!!!

i always use system("pause"); to hold the black srceen open. but it has no effect. u can reply to this if you have any other suggestion but i wont be able to answer for another hour or so...but pls any help will be greatly appreciated!

i always use system("pause"); to hold the black srceen open. but it has no effect. u can reply to this if you have any other suggestion but i wont be able to answer for another hour or so...but pls any help will be greatly appreciated!

Why use system("pause"); to call the operating system and execute an OS command when a simple cin will stop execution until you press ENTER? Isn't cin actually part of the language itself?

Why use system("pause"); to call the operating system and execute an OS command when a simple cin will stop execution until you press ENTER? Isn't cin actually part of the language itself?

cuz system("pause") holds the screen , even after i have input everything i need to input into the black screen.

Member Avatar for Member #46692

What IDE are you using?

What IDE are you using?

i use Dev C++.

cuz system("pause") holds the screen , even after i have input everything i need to input into the black screen.

I don't understand. How does cin fail to "hold the screen"? And where does the "black screen" come from using system("pause")?

I don't understand. How does cin fail to "hold the screen"? And where does the "black screen" come from using system("pause")?

cin doesnt fail to hold the screen. it's just i prefer system ("pause").
the difference between them(that i know of) is ...cin requires input after the code is compiled and run, while system("pause") just holds it without any"demand" and pressing any key closes the screen.
main thing is i prefer system("pause").

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.