using c++ language divide program to main.cpp,animal.h,animal.cpp need use \" g+
ID: 3742640 • Letter: U
Question
using c++ language
divide program to main.cpp,animal.h,animal.cpp
need use "g++ -o count animal.cpp main.cpp "in terminal to compile
Part 1 - Simple Classes and Class Variables
1-1
Define and implement a class named animal that has the following public constructors and behaviours:
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 currentID and that must be initialised to 0, the animal.h file must contain:
and the animal.cpp file must contain (outside of any of the method implementations):
You can access the currentID variable when you are creating objects of class animal. Think carefully about this use. currentID 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 currentID 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.
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
//animal.h
#ifndef animal_h
#define animal_h
#include<iostream>
using namespace std;
//animal class
class animal{
private:
//basic attributes
int id;
string species;
string name;
public:
//static variable to keep the track of id
static int currentId;
//constructor with argument to initialize species field
animal(string species);
//method to set name
void set_name(string name);
//getter methods
string get_species();
string get_name();
int get_id();
};
#endif
//animal.cpp
#include "animal.h"
//initializing static variable
int animal::currentId=0;
//constructor implementation
animal::animal(string spec){
species=spec;
//assigning next unique id to the animal, also updating the current id
id=++currentId;
}
void animal::set_name(string nm){
name=nm;
}
string animal::get_species(){
return species;
}
string animal::get_name(){
return name;
}
int animal::get_id(){
return id;
}
//main.cpp
#include "animal.h"
int main(){
//creating two animals
animal cheetah("Cheetah");
animal elephant("Elephant");
//giving two names for the two animals
cheetah.set_name("Bob");
elephant.set_name("Jim");
//displaying details of two animals
cout<<cheetah.get_name()<<", species: "<<cheetah.get_species()<<", id: "<<cheetah.get_id()<<endl;
cout<<elephant.get_name()<<", species: "<<elephant.get_species()<<", id: "<<elephant.get_id()<<endl;
return 0;
}
/*OUTPUT*/
Bob, species: Cheetah, id: 1
Jim, species: Elephant, id: 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.