Base class constructor:
Code:
Port(const char* brand = "none", const char* style = "none", int bottles = 0)
Derived class constructor:
Code:
VintagePort(const char* brand = "none", int bottles = 0, const char* nickname = "none", int year = 0)
Vintage is a style of port and hence style has been removed from the derived class, VintagePort, constructor.
I have written the derived class constructor as follows:
Code:
VintagePort::VintagePort(const char* brand, int bottles, const char* nickname, int year){
Port::SetBrand(brand);
Port::SetBottles(bottles);
m_nickname = new char[strlen(nickname)+1];
strcpy(m_nickname, nickname);
m_year = year;
}
But I am not sure how to write the derived class copy constructor. Here is my unsuccessful attempt and any advice would be most welcome:
Code:
VintagePort::VintagePort(const VintagePort& vp){
this->GetBrand() = new char [strlen(vp.Port::GetBrand())+1)]
}
FYI, the problem asks to use arrays to practice dynamic memory allocation.
PS:
The function names are self-explanatory; nevertheless for the sake of completeness, here is the full code so far:
Code:
#include<iostream>
#include<cstring>
using namespace std;
class Port{
private:
char* m_brand;
char m_style[20];
int m_bottles;
public:
Port(const char* brand = "none", const char* style = "none", int bottles = 0);
Port(const Port& p); // copy constructor;
virtual ~Port(){delete[]m_brand;}
Port& operator=(const Port& p);
Port& operator+=(int b);// adds b to bottles;
Port& operator-=(int b);//subtracts b from bottles if available;
int BottleCount() const { return m_bottles;};
void SetBrand(const char* brand = "none");
char* GetBrand(){return m_brand;};
void SetBottles(int bottles){m_bottles = bottles;};
int GetBottles(){return m_bottles;};
virtual void Show()const;
friend ostream& operator<<(ostream& os, const Port& p);
};
class VintagePort: public Port{
private:
char* m_nickname;
int m_year;
public:
VintagePort();
VintagePort(const char* brand = "none", int bottles = 0, const char* nickname = "none", int year = 0);
VintagePort(const VintagePort& vp);
~VintagePort(){delete []m_nickname;};
VintagePort& operator=(const VintagePort& vp);
void Show()const{};
friend ostream& operator<<(ostream& os, const VintagePort& vp);
};
int main(){}
Port::Port(const char* brand , const char* style , int bottles ){
m_brand = new char[strlen(brand)+1];
strcpy(m_brand, brand);
strncpy(m_style, style, 19);
m_style[19]='\0';
m_bottles = bottles;
}
Port::Port(const Port& p){
m_brand = new char[strlen(p.m_brand)+1];
strcpy(m_brand, p.m_brand);
strncpy(m_style, p.m_style, 19);
m_style[19]='\0';
m_bottles = p.m_bottles;
}
Port& Port::operator=(const Port& p){
if(this ==&p){
return *this;
}
delete m_brand;
m_brand = new char[strlen(p.m_brand)+1];
strcpy(m_brand, p.m_brand);
for (int i = 0; i<20; i++){
m_style[i]='\0';
}
strncpy(m_style, p.m_style, 19);
m_style[19]='\0';
m_bottles='\0';
m_bottles = p.m_bottles;
return *this;
}
Port& Port::operator+=(int b){
m_bottles +=b;
}
Port& Port::operator-=(int b){
if (m_bottles>=b){
m_bottles -=b;
} else
m_bottles = 0;
}
void Port::Show()const{
cout<<"The brand is:"<<m_brand<<" of style:"<<m_style<<" and there are:"<<m_bottles<<" bottles in the collection"<<endl;
}
ostream& operator<<(ostream& os, const Port& p){
os<<"The brand is:"<<p.m_brand<<" of style:"<<p.m_style<<" and there are:"<<p.m_bottles<<" bottles in the collection"<<endl;
return os;
}
void Port::SetBrand(const char* brand){
m_brand = new char[strlen(brand)+1];
strcpy(m_brand, brand);
}
VintagePort::VintagePort(const char* brand, int bottles, const char* nickname, int year){
Port::SetBrand(brand);
Port::SetBottles(bottles);
m_nickname = new char[strlen(nickname)+1];
strcpy(m_nickname, nickname);
m_year = year;
}
VintagePort::VintagePort(const VintagePort& vp){
this->GetBrand() = new char [strlen(vp.Port::GetBrand())+1)]
}