new in classes

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;


Dog::Dog(void)
{
}


Dog::~Dog(void)
{
}

int Dog::getAge()
{
return itsAge;
}
int Dog::getWeight()
{
return itsWeight;
}
void Dog::setAge(int Age)
{
itsAge = Age;
}
void Dog::setWeight(int Weight)
{
itsWeight = Weight;
}
void Dog::bark()
{
cout << "Bark, bark.....";
}
Last edited on
If they all have different values, you have to do it this way.
Thx :)
But I have another question.. What is the difference and result of treating class as an object ? And when we do that ?
Last edited on
Classes are not objects. Instances of classes are objects.
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 ?
Last edited on
All classes are static in C++, or maybe I am not understanding what you mean? Can you give a short example?
ResidentBiscuit wrote:
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...
1
2
3
    Dog dog1;
    dog1.setAge(5);
    dog1.setName("Pigwa");
Last edited on
I give you a code.....

//main

#include <iostream>
#include "PrimeNUMBER.h"
using namespace std;

int main()
{

PrimeNUMBER eNumber;
int n;
cout << "Enter a number: ";
cin >> n;
eNumber.setNumber(n);
eNumber.checkNumber(n);

system("Pause");

return 0;
}

// class

class PrimeNUMBER
{
public:
PrimeNUMBER(void);
~PrimeNUMBER(void);
void setNumber(int n) { Liczba = n; }
bool ifPrimeNumber(int n);
void factor(int n);
void nextPrime(int n);
void userPrime(int n);
void checkNumber (int n);

private:
int Liczba;
};

// cpp

#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";

else
cout << "The number is not prime.\n";
}

void PrimeNUMBER::checkNumber(int n)
{
userPrime (n);
cout << "\nFactors: ";
factor(n);
cout << endl;
nextPrime(n);
cout << endl;
}

//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...
Last edited on
LB wrote:
All classes are static in C++

Huh?

Chervil wrote:

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.
Last edited on
@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/
Last edited on
A "class" is a class, an "instance of a class" is an object.
Oki LB, thx :-))))
I did understand Chervil, thank you very much :)
Topic archived. No new replies allowed.