vendredi 22 juillet 2016

C++ code - needed for exam revision

I have tried to create a dog class that returns the name and type of a dog.

but for some reason the code doesent seem to work and returns the message "error: 'name' was not declared in this scope". (when i try to print out the name as shown in the code below).

im really unconfident with c++, you can probably tell by my code itself, even when creating this class i did it by following a similar class provided to me by a lecturer. the question i am trying to learn how to answer asks the user to create a class which contains a constructor, getter methods, and changes the type of something...also it asks the user to create an object in main for this class.

im studying computer science at university, and i failed my exam for c++ (clearly!), apart from this code, if anyone has any advice on how i can learn to program/pass the c++ exam id really appreciate it...bearing in mind i only have around 3 weeks left to the exam, and the pass mark must be 40/100.

thank you!

using namespace std;

class dog {

private: string name; int type;

public:

dog(string d) : name(d), type(0){}

dog(string d, int type_of_dog): name(d), type(type_of_dog){}

string get_name() const {return name;}

int get_type() const {return type;}

void change(int type) {

type += type;
}

};

int main() {

dog safron("safron");
dog.name = d;

cout << "this is the name of the dog: " << name << endl;



return 0;

}

Aucun commentaire:

Enregistrer un commentaire