Lab 10 Consider the following class definition public Cow(const char\" n, double
ID: 3740688 • Letter: L
Question
Lab 10 Consider the following class definition public Cow(const char" n, double w) Cow(const Cow& c) oid ShowCow) const; double weight display all cow data ation for this class in a epp Sle) and wrte a short program the uses allthe memberfunctions. The prints the name of the function, for example first line of each function should be a cout statement that Because name is a pointer to a character string, you will need to do a DEEP COPY in any function that copies the name of the Cow. This means using new and delete in the appropriate places. A pointer that has been set to nullptr can be passed to delete and delete wil not 1. Project/solution named correctly 2. Correct comments at top 3. Consistent indentation (Use Edit/Advanced/Format Document 4. Good variable names 5. Overall neat organization 6 Comments in code explain what's being done 7 Correct division of code into h and.cpp files 8 Use of ilpagma once in h files (or, #fhdenExplanation / Answer
Cow.h
#pragma once
#ifndef COW_H
#define COW_h
#include<iostream>
#include<cstring>
using namespace std;
class Cow
{
public:
Cow();
Cow(const char *n, double w);
Cow(const Cow &c);
~Cow();
Cow &operator=(const Cow &c);
void ShowCow() const;
private:
char *name;
double weight;
};
#endif
Cow.cpp
#include "Cow.h"
Cow::Cow(){
weight = 0.0;
}
Cow::Cow(const char *n, double w){
name = new char[100];
strcpy(name, n);
weight = w;
}
Cow::Cow(const Cow &c){
name = new char[100];
strcpy(name, c.name);
weight = c.weight;
}
Cow::~Cow(){
delete this;
}
Cow &Cow::operator=(const Cow &c){
Cow *new_c = new Cow(c);
return *new_c;
}
void Cow::ShowCow() const{
cout<<"Name: "<<name<<", Weight: "<<weight<<endl;
}
main.cpp
#include<iostream>
#include "Cow.cpp"
using namespace std;
int main(){
Cow *c = new Cow();
Cow *c1 = new Cow("Jersey", 100);
cout<<" Through constructor: ";
c1->ShowCow();
c = c1;
cout<<"Assigning: ";
c->ShowCow();
cout<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.