Question

Part 1 - Simple Classes and Class Variables 1-1 Define and implement a class named animal that has the following public constructors and behaviours animal (string aSpecies); // animals are allocated a unique ID on creation, // the first animal has ID 1, the second animal is 2 and so on void set_name(string aName); // change the animals name string get_speciesO; string get nameO; int get_IDO; // the animals unique ID This should be straight forward except for the ID. How do we keep track of what ID number we are up to? The solution to this problem is to declare a class variable as we saw in the Panda example. A class variable as its name implies is a variable that belongs to the class itself, not object instances. It is declared in the class declaration (just like other state variable) but is identified as a class variable by the keyword static. There is only one copy of this variable no matter how many object instances are created Class variables are declared in the classname>,h file but they cannot be initialised in a constructor and must be initialised in the <classname>.cpp file. For example, if the animal class has a class variable that is named currentlD and that must be initialised to 0, the animal.h file must contain static int currentID; // the next id number to give out and the animal.cpp file must contain (outside of any of the method implementations) int animal: : current!D = 0 You can access the currentID variable when you are creating objects of class animal. Think carefully about this use currentlD should be 1 when you create your first animal, 2 when you create your second animal, and so on. What will you need to do to currentlD whenever you create an animal? Each animal must have their own unique ID. Do they need their own state variable to hold this unique ID or can they use currentID? Your main program should create two animal objects, an Elephant and a Cheetah, then display their details. Your main program should demonstrate that the name can be changed

In c++ please and a correct answer please because last time some guy gave me a wrong code. Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Each animal will have their own unique ID variable. But there will also be a static variable which will be incremented every time an object is created. And then it will be assigned to the ID of that object.

main.cpp

#include<iostream>
using namespace std;
class animal
{
string specie; //specie of animal
string name; //name of animal
int ID; //unique id of animal
public:
static int counter; //static variable for ID
animal(string aSpecies) //constructor for setting ID and species
{
counter++; //incrementing counter by 1
ID=counter; //assigning counter to ID
specie=aSpecies;
}
void set_name(string aName) //constructor for setting name
{
name=aName;
}
string get_species() //function to get specie
{
return specie;
}
string get_name() //function to get name
{
return name;
}
int getID() //function to get ID
{
return ID;
}
};
int animal::counter=0; //initializing static variable to 0
int main()
{
animal elephant("asian");
elephant.set_name("elephant");
animal cheetah("african");
cheetah.set_name("cheetah");
cout<<"Elephant details: \n" ;
cout<<elephant.getID()<<endl<<elephant.get_name()<<endl<<elephant.get_species();
cout<<endl<<"Cheetah details: \n";
cout<<cheetah.getID()<<endl<<cheetah.get_name()<<endl<<cheetah.get_species();

return 0;

}


CAUserslsam computers Documentslvehicle.exe Elephant details: elephant sian heetah details: heetalh frican Process returned 0 (0x0) execut 10n tine : 0.018 s ss any key to continue.}

Add a comment
Know the answer?
Add Answer to:
In c++ please and a correct answer please because last time some guy gave me a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need some help i need to do this in C# Objectives: • Create an application...

    I need some help i need to do this in C# Objectives: • Create an application that uses a dictionary collection to store information about an object. • Understanding of abstract classes and how to use them • Utilize override with an abstract class • Understanding of Interfaces and how to use them • Implement an Interface to create a “contract” between classes. • Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • Please help me with the following question. This is for Java programming. In this assignment you...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the...

  • In header file (.h) and c++ file format (.cpp). A local company has asked you to...

    In header file (.h) and c++ file format (.cpp). A local company has asked you to write a program which creates an Employee class, a vector of Employee class objects, and fills the objects with employee data, creating a "database" of employee information. The program allows the user to repeatedly search for an employee in the vector by entering the employee's ID number and if found, display the employee's data. The Employee_C class should have the following data and in...

  • C++ This exercise will introduce static member variables and static methods in class to you. Class...

    C++ This exercise will introduce static member variables and static methods in class to you. Class Department contain information about universities departments: name students amount in addition it also stores information about overall amount of departments at the university: departments amount class Department { public: Department(string i_name, int i_num_students); ~Department(); int get_students(); string get_name(); static int get_total(); private: string name; int num_students; static int total_departments; }; Carefully read and modify the template. You have to implement private static variable "total...

  • Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • COSC 1437 C++2 Project Assignment 3 Description: Computer Science Department is evaluating its professors to see...

    COSC 1437 C++2 Project Assignment 3 Description: Computer Science Department is evaluating its professors to see which professor has the highest rating according to student input. You will create a ProfessorRating class consisting of professor Name and three ratings. The three ratings are used to evaluate easiness, helpfulness, and clarity. The value for each rating is in the range of 1 to 5, with 1 being the lowest and 5 being the highest. Your program should contain the following functionalities:...

  • Please give me that correct code for this by using C++ language. and i hope you...

    Please give me that correct code for this by using C++ language. and i hope you use Visual stduio Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }.  For example, the string  {a(b+ac)d[xy]g} and  kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT