Why is this not working PLEASE HELP DUE at MIDNIGHT I have the code but it is no
ID: 3700717 • Letter: W
Question
Why is this not working PLEASE HELP DUE at MIDNIGHT
I have the code but it is not compiling and has many error messages could someone help, I need to compile the 3 cpp files with the g++ and it will not work... Please show its working thank you!
CODE:
lab8dog.h
#include <iostream>
using namespace std;
class Dog
{
protected:
string name;
string color;
double weight;
public:
Dog();
Dog(string,double,string);
virtual void displayDog();
virtual ~Dog();
};
lab8show.h
class ShowDog:public Dog
{
private:
int noContests;
int winContests;
public:
ShowDog();
ShowDog(string,double,string,int,int);
void displayDog();
};
lab8dog.cpp
#include "lab8dog.h"
Dog::Dog(){
name="unknown";
color="unknown";
weight=0.0;
}
Dog::Dog(string n,double w,string c){
name=n;
color=c;
weight=w;
}
void Dog::displayDog(){
cout<<" Dog's name: "<<this->name;
cout<<" Dog's color: "<<color;
cout<<" Dog's weight: "<<weight;
}
Dog::~Dog(){
// cout<<"Destructor called"<<endl;
}
lab8show.cpp
#include "lab8show.h"
#include <iostream>
using namespace std;
ShowDog::ShowDog():Dog()
{
noContests=0;
winContests=0;
}
ShowDog::ShowDog(string n,double w,string c,int total,int win): Dog(n,w,c)
{
noContests=total;
winContests=win;
}
void ShowDog::displayDog()
{
cout<<" ";
Dog::displayDog();
cout<<" "<<name<<" entered in "<<noContests<<" contests";
cout<<" " << name<<" won "<<winContests<<" contests ";
}
lab8client.cpp
#include "lab8show.cpp"
int main(){
Dog D("Bolt", 20,"White");
D.displayDog();
ShowDog SD("Uggie", 30, "Blk/Wht", 3, 1 );
SD.displayDog();
Dog *dogList[4];
Dog *temp;
// temp=new Dog("Bingo", 20, "Tan");
dogList[0]=new Dog("Bingo", 20, "Tan");;
dogList[1]= new ShowDog("Beethoven", 200, "Brown",3,1);
dogList[2]= new ShowDog("Marley", 80, "Yellow",10,3);
dogList[3]= new Dog("Fido", 50, "Rust");
for(int i=0;i<4;i++){
dogList[i]->displayDog();
cout<<" ";
}
return 0;
}
Explanation / Answer
Solution:
Please try this working code:
lab8dog.h
#ifndef LAB8DOG_H
#define LAB8DOG_H
#include <string>
class lab8dog
{
protected:
// Data members
std::string name;
std::string color;
double weight;
public:
lab8dog(); // Default constructor
// Parametrized constructor with 3 arguments
lab8dog(std::string n, std::string c, double w);
void displayDog();
};
#endif // LAB8DOG_H
lab8dog.cpp
#include "lab8dog.h"
#include<iostream>
#include<iomanip>
using namespace std;
// Set default values to data members
lab8dog::lab8dog()
{
name="unknown";
color="unknown";
weight =0.0;
}
// Set passed values to data members
lab8dog::lab8dog(string n, string c, double w)
{
name=n;
color=c;
weight=w;
}
void lab8dog::displayDog()
{
// Display dog's name, color and weight
cout<<"Dog's Name: "<<name<<endl;
cout<<"Dog's Color: "<<color<<endl;
cout<<"Dog's Weight: "<<fixed<<setprecision(2)<<weight<<endl;
}
lab8show.h
#ifndef LAB8SHOW_H
#define LAB8SHOW_H
#include "lab8dog.h"
// Inherit lab8dog class
class lab8show:public lab8dog
{
public:
lab8show(); // Default constructor
// Parametrized constructor with 5 arguments
lab8show(std::string name, std::string color, double weight,int numberOfContests , int contestWon);
void displayDog();
private:
// Data members
int numberOfContests;
int contestWon;
};
#endif // LAB8SHOW_H
lab8show.cpp
#include "lab8show.h"
#include<iostream>
using namespace std;
// Set default values to data members
// call base class constructor as well
lab8show::lab8show():lab8dog()
{
numberOfContests =0;
contestWon = 0;
}
// Set passed values to data members
// Call base class constructor with 3 arguments
lab8show::lab8show(string name, string color, double weight,int n, int won):lab8dog(name,color,weight)
{
numberOfContests = n;
contestWon = won;
}
void lab8show::displayDog(){
// Display details
lab8dog::displayDog(); // call base class method
cout<<lab8dog::name<<" was entered in "<<numberOfContests<<" contests,"<<endl;
cout<<lab8dog::name<<" won "<<contestWon<<" of the contests"<<endl;
}
main.cpp
#include <iostream>
#include "lab8dog.h"
#include "lab8show.h"
using namespace std;
int main()
{
// Make several objects of both classes and
// call function displayDog on them
lab8dog d1{"Bolt","White",20};
d1.displayDog();
cout<<endl;
lab8show s1{"Uggie","Blk/Wht",30,3,1};
s1.displayDog();
cout<<endl;
lab8dog d2{"Bingo","Tan",20};
d2.displayDog();
cout<<endl;
lab8show s2{"Beethoven","Brown",200,3,1};
s2.displayDog();
cout<<endl;
lab8show s3{"Marley","Yellow",80,10,3};
s3.displayDog();
cout<<endl;
lab8dog d3{"Fido","Rust",50};
d3.displayDog();
cout<<endl;
return 0;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.