I need your help. I've just started classes.. I have got such code and I do not want to write so many times the same think for 5 dogs....;-), how to make this code simpler ?
My code:
//main
#include <iostream>
#include "Dog.h"
using namespace std;
int main()
{
Dog Pigwa, Burek, Ron, Zuza, Don;
Pigwa.setAge(5);
cout << "Pigwa is : " << Pigwa.getAge() << " years old\n";
Pigwa.setWeight(20);
cout << "Pigwa is : " << Pigwa.getWeight() << " kg weight\n";
Pigwa.bark();
cout << endl;
Burek.setAge(8);
cout << "Burek is: " << Burek.getAge() << " years old\n";
Burek.setWeight(12);
cout << "Burek is: " << Burek.getWeight() << " kg weight\n";
Burek.bark();
cout << endl;
Ron.setAge(10);
cout << "Ron is: " << Ron.getAge() << " years old\n";
Ron.setWeight(15);
cout << "Ron is: " << Ron.getWeight() << " kg weight\n";
Ron.bark();
cout << endl;
Zuza.setAge(5);
cout << "Zuza is : " << Zuza.getAge() << " years old\n";
Zuza.setWeight(19);
cout << "Zuza is : " << Zuza.getWeight() << " kg weight\n";
Zuza.bark();
cout <<endl;
Don.setAge(5);
cout << "Don is : " << Don.getAge() << " years old\n";
Don.setWeight(18);
cout << "Don is : " << Don.getWeight() << " kg weight\n";
Don.bark();
cout << endl;
system ("Pause");
return 0;
}
//class
class Dog
{
public:
Dog(void);
~Dog(void);
int getAge();
int getWeight();
void setAge(int Age);
void setWeight(int Weight);
void bark();
private:
int itsAge;
int itsWeight;
};
//cpp
#include "Dog.h"
#include <iostream>
using namespace std;
I know, but I was told once that I treat class as an object, and I did not understand...../ I just put my code/
static, and non static class might be w source of problem ?
If they all have different values, you have to do it this way.
Well, there is a lot of repeated code, that would suggest possibly a loop, or perhaps another function to carry out the code which is common to all cases.
Also, rather than do this:
1 2 3
Dog Pigwa;
Pigwa.setAge(5);
cout << "Pigwa is : " << Pigwa.getAge() << " years old\n";
It might be more flexible to do this:
1 2 3 4
Dog dog1;
dog1.setAge(5);
dog1.setName("Pigwa");
cout << dog1.getName() << " is : " << dog1.getAge() << " years old\n";
then the multiple objects could be handled as an array.
and with a suitable constructor these three lines could be done in a single statement...
#include "PrimeNUMBER.h"
#include <iostream>
#include <cmath>
using namespace std;
int minNumber = 2;
PrimeNUMBER::PrimeNUMBER(void)
{
}
PrimeNUMBER::~PrimeNUMBER(void)
{
}
bool PrimeNUMBER::ifPrimeNumber(int n)
{
int limit = sqrt(double(n));
for (int i = minNumber; i <= limit ;i++ )
{
if (n % i == 0)
return false;
}
return true;
}
void PrimeNUMBER::factor(int n)
{
int limit = sqrt(double(n));
for (int i = minNumber; i <= limit; i++)
if (n % i == 0)
{
cout << i << ", ";
factor(n / i);
return;
}
cout << n;
}
void PrimeNUMBER::nextPrime (int n)
{
int nextNumber = ++n;
while (!ifPrimeNumber(nextNumber))
{
++nextNumber;
}
cout << "\nNext prime number: " << nextNumber;
}
void PrimeNUMBER::userPrime(int n)
{
if (ifPrimeNumber(n))
cout << "The number is prime.\n";
//what is wrong with this code ?
// sorry I am Polish.. Liczba means number :)
//I was told then I treat class as an object, and I did not understand why...
that would suggest possibly a loop, or perhaps another function to carry out the code which is common to all cases.
If there's something that's common to all Dogs on creation, then that value should just be default. Otherwise, you'll be doing what OP is already doing and just assigning values after creation.
usmiech wrote:
what is wrong with this code?
I don't know, what issue is it having? We're not gonna search this for an error somewhere.
@ResidentBiscuit: in other languages such as Java, nested classes can be either static (not associated with an instance of the outer class) or non-static (they are associated with an instance of the outer class). In C++, all classes are static because nested class instances are not associated with instances of the outer class.
Thx Resident, I will find a right loop :)/fe with switch..../, but I do not understand why I was told to treat a class as an object....
Therefore I gave this example../code/