728x90
1๋ฒ
//classic.h
#ifndef CLASSIC_H_
#define CLASSIC_H_
class Cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;
public:
Cd(char* s1="no name", char* s2="no label", int n=0, double x=0.0);
//Cd(const Cd& d);
virtual ~Cd() {}
virtual void Report() const;
Cd& operator=(const Cd& d);
};
class Classic :public Cd
{
private:
char title[50];
public:
Classic(char* s3 = "no title",char* s1="no name", char* s2="no label", int n=0,
double x=0.0);
Classic(const Cd& d, const char* s3);
Classic& operator=(const Classic& d);
virtual void Report() const;
};
#endif
//classic.cpp
#include"classic.h"
#include<iostream>
#include<cstring>
Cd::Cd(char* s1, char* s2, int n, double x)
{
strcpy(performers, s1);
strcpy(label, s2);
selections = n;
playtime = x;
}
//Cd(const Cd& d);
void Cd::Report() const
{
std::cout << "์ฐ์ฃผ์ : " << performers
<< "\n๋ ์ด๋ธ : " << label
<< "\n์๋ก ๊ณก๋ชฉ ์ : " << selections
<< "\n์ฐ์ฃผ ์๊ฐ : " << playtime<<'\n';
}
Cd& Cd::operator=(const Cd& d)
{
if (this == &d)
return *this;
strcpy(performers, d.performers);
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic(char* s3,char* s1, char* s2, int n, double x) :Cd(s1, s2, n, x)
{
strcpy(title, s3);
}
Classic::Classic(const Cd& d, const char* s3) :Cd(d)
{
strcpy(title, s3);
}
Classic& Classic::operator=(const Classic& d)
{
if (this == &d)
return *this;
Cd::operator=(d);
strcpy(title,d.title);
return *this;
}
void Classic::Report() const
{
std::cout << "๋ํ๊ณก : " << title << '\n';
Cd::Report();
}
//pe13-01.cpp
#include<iostream>
using namespace std;
#include"classic.h"
void Bravo(const Cd& disk);
int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel",
"Philips", 2, 57.17);
Cd* pcd = &c1;
cout << "๊ฐ์ฒด๋ฅผ ์ง์ ์ฌ์ฉํ๋ค:\n";
c1.Report();
c2.Report();
cout << "๊ฐ์ฒด๋ฅผ ์ง์ํ๋ Cd* ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ค:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "Cd ์ฐธ์กฐ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ํจ์๋ฅผ ํธ์ถํ๋ค:\n";
Bravo(c1);
Bravo(c2);
cout << "๋์
์ ํ
์คํธ ํ๋ค:\n";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd& disk)
{
disk.Report();
}
2๋ฒ
//classic1.h
#ifndef CLASSIC1_H_
#define CLASSIC1_H_
class Cd
{
private:
char *performers;
char *label;
int selections;
double playtime;
public:
Cd(char* s1="no name", char* s2="no label", int n=0, double x=0.0);
//Cd(const Cd& d);
virtual ~Cd();
virtual void Report() const;
Cd& operator=(const Cd& d);
};
class Classic :public Cd
{
private:
char *title;
public:
Classic(char* s3 = "no title",char* s1="no name", char* s2="no label", int n=0,
double x=0.0);
Classic(const Cd& d, const char* s3);
~Classic();
Classic& operator=(const Classic& d);
virtual void Report() const;
};
#endif
//classic1.cpp
#include"classic1.h"
#include<iostream>
#include<cstring>
Cd::Cd(char* s1, char* s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
label = new char[strlen(s2) + 1];
strcpy(performers, s1);
strcpy(label, s2);
selections = n;
playtime = x;
}
//Cd(const Cd& d);
void Cd::Report() const
{
std::cout << "์ฐ์ฃผ์ : " << performers
<< "\n๋ ์ด๋ธ : " << label
<< "\n์๋ก ๊ณก๋ชฉ ์ : " << selections
<< "\n์ฐ์ฃผ ์๊ฐ : " << playtime<<'\n';
}
Cd::~Cd()
{
delete[]performers;
delete[]label;
}
Cd& Cd::operator=(const Cd& d)
{
if (this == &d)
return *this;
delete[]performers;
delete[]label;
performers = new char[strlen(d.performers) + 1];
label = new char[strlen(d.label) + 1];
strcpy(performers, d.performers);
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic(char* s3,char* s1, char* s2, int n, double x) :Cd(s1, s2, n, x)
{
title = new char[strlen(s3) + 1];
strcpy(title, s3);
}
Classic::Classic(const Cd& d, const char* s3) :Cd(d)
{
delete[]title;
title = new char[strlen(s3) + 1];
strcpy(title, s3);
}
Classic::~Classic()
{
delete[]title;
}
Classic& Classic::operator=(const Classic& d)
{
if (this == &d)
return *this;
Cd::operator=(d);
delete[]title;
title = new char[strlen(d.title) + 1];
strcpy(title,d.title);
return *this;
}
void Classic::Report() const
{
std::cout << "๋ํ๊ณก : " << title << '\n';
Cd::Report();
}
//pe13-02.cpp
#include<iostream>
using namespace std;
#include"classic1.h"
void Bravo(const Cd& disk);
int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel",
"Philips", 2, 57.17);
Cd* pcd = &c1;
cout << "๊ฐ์ฒด๋ฅผ ์ง์ ์ฌ์ฉํ๋ค:\n";
c1.Report();
c2.Report();
cout << "๊ฐ์ฒด๋ฅผ ์ง์ํ๋ Cd* ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ค:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "Cd ์ฐธ์กฐ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ํจ์๋ฅผ ํธ์ถํ๋ค:\n";
Bravo(c1);
Bravo(c2);
cout << "๋์
์ ํ
์คํธ ํ๋ค:\n";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd& disk)
{
disk.Report();
}
3๋ฒ
//dmaabc.h
#ifndef DMAABC_H_
#define DMAABC_H_
#include<iostream>
class Dmaabc
{
private:
char* label;
int rating;
public:
Dmaabc(const char* l = "null", int r = 0);
Dmaabc(const Dmaabc& rs);
virtual ~Dmaabc();
virtual Dmaabc& operator=(const Dmaabc& rs);
friend std::ostream& operator<<(std::ostream& os,
const Dmaabc& rs);
virtual void View() const = 0;
};
class baseDMA :public Dmaabc
{
public:
baseDMA(const char* l = "null", int r = 0) : Dmaabc(l, r) {}
baseDMA(const baseDMA& rs);
virtual void View() const;
virtual ~baseDMA() {}
virtual baseDMA& operator=(const baseDMA& rs);
friend std::ostream& operator<<(std::ostream& os, const baseDMA& rs);
};
class lacksDMA : public Dmaabc
{
private:
enum { COL_LEN = 40 };
char color[COL_LEN];
public:
lacksDMA(const char* c = "blank", const char* l = "null",
int r = 0);
lacksDMA(const char* c, const Dmaabc& rs);
lacksDMA(const lacksDMA& rs);
virtual ~lacksDMA() {}
virtual void View() const;
virtual lacksDMA& operator=(const lacksDMA& rs);
friend std::ostream& operator<<(std::ostream& os,
const lacksDMA& rs);
};
class hasDMA :public Dmaabc
{
private:
char* style;
public:
hasDMA(const char* s = "none", const char* l = "null",
int r = 0);
hasDMA(const char* s, const Dmaabc& rs);
hasDMA(const hasDMA& hs);
virtual ~hasDMA();
virtual hasDMA& operator=(const hasDMA& rs);
friend std::ostream& operator<<(std::ostream& os,
const hasDMA& rs);
virtual void View() const;
};
#endif
//dmaabc.cpp
#include "dmaabc.h"
#include<cstring>
Dmaabc::Dmaabc(const char* l, int r)
{
label = new char[strlen(l) + 1];
strcpy(label, l);
rating = r;
}
Dmaabc::Dmaabc(const Dmaabc& rs)
{
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
}
Dmaabc::~Dmaabc()
{
delete[]label;
}
Dmaabc& Dmaabc::operator=(const Dmaabc& rs)
{
if (this == &rs)
return *this;
delete[]label;
label = new char[strlen(rs.label) + 1];
strcpy(label, rs.label);
rating = rs.rating;
return *this;
}
std::ostream& operator<<(std::ostream& os,
const Dmaabc& rs)
{
os << "์ํ : " << rs.label << '\n'
<< "๋ฑ๊ธ : " << rs.rating << '\n';
return os;
}
void Dmaabc::View() const
{
std::cout<< "label : " << label << '\n'
<< "rating : " << rating << '\n';
}
baseDMA::baseDMA(const baseDMA& rs) :Dmaabc(rs)
{}
void baseDMA::View() const
{
Dmaabc::View();
}
baseDMA& baseDMA::operator=(const baseDMA& rs)
{
if (this == &rs)
return *this;
Dmaabc::operator=(rs);
return *this;
}
std::ostream& operator<<(std::ostream& os, const baseDMA& rs)
{
os << (const Dmaabc&)rs << '\n';
return os;
}
lacksDMA::lacksDMA(const char* c, const char* l,int r) :Dmaabc(l, r)
{
strncpy(color, c, 39);
color[39] = '\0';
}
lacksDMA::lacksDMA(const char* c, const Dmaabc& rs) : Dmaabc(rs)
{
strncpy(color, c, 39);
color[39] = '\0';
}
lacksDMA::lacksDMA(const lacksDMA& rs) :Dmaabc(rs)
{
strncpy(color, rs.color, 39);
color[39] = '\0';
}
void lacksDMA::View() const
{
Dmaabc::View();
std::cout << "color : " << color << '\n';
}
lacksDMA& lacksDMA::operator=(const lacksDMA& rs)
{
if (this == &rs)
return *this;
Dmaabc::operator=(rs);
strncpy(color, rs.color, 39);
color[39] = '\0';
return *this;
}
std::ostream& operator<<(std::ostream& os, const lacksDMA& rs)
{
os << (const Dmaabc&)rs;
os << "์์ : " << rs.color << '\n';
return os;
}
hasDMA::hasDMA(const char* s, const char* l, int r) :Dmaabc(l, r)
{
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const char* s, const Dmaabc& rs) : Dmaabc(rs)
{
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA& hs) :Dmaabc(hs)
{
style = new char[strlen(hs.style) + 1];
strcpy(style, hs.style);
}
hasDMA::~hasDMA()
{
delete[] style;
}
hasDMA& hasDMA::operator=(const hasDMA& rs)
{
if (this == &rs)
return *this;
Dmaabc::operator=(rs);
delete[] style;
style = new char[strlen(rs.style) + 1];
strcpy(style, rs.style);
return *this;
}
std::ostream& operator<<(std::ostream& os, const hasDMA& rs)
{
os << (const Dmaabc&)rs;
os << "์คํ์ผ : " << rs.style << '\n';
return os;
}
void hasDMA::View() const
{
Dmaabc::View();
std::cout << "style : " << style << '\n';
}
//pe13-03.cpp
#include<iostream>
#include"dmaabc.h"
int main()
{
using std::cin;
using std::cout;
using std::endl;
Dmaabc* p_dma[3];
char temp[50];
int r;
char kind;
for (int i = 0; i < 3; i++)
{
cout << "์ํ ์ด๋ฆ์ ์
๋ ฅํ์ญ์์ค: ";
cin.getline(temp,50);
cout << "๋ฑ๊ธ์ ์
๋ ฅํ์ญ์์ค: ";
cin >> r;
cout << "baseDMA : 1, lacksDMA : 2, hasDMA : 3\n";
(cin >> kind).get();
if (kind == '1')
p_dma[i] = new baseDMA(temp, r);
else if (kind == '2')
{
char color[40];
cout << "์์ ์
๋ ฅ : ";
cin.getline(color, 40);
p_dma[i] = new lacksDMA(color, temp, r);
}
else
{
char style[50];
cout << "์คํ์ผ ์
๋ ฅ : ";
cin.getline(style, 50);
p_dma[i] = new hasDMA(style, temp, r);
}
}
cout << endl;
cout << "View ํจ์๋ฅผ ์ด์ฉํ ์ถ๋ ฅ\n";
for (int i = 0; i < 3; i++)
{
p_dma[i]->View();
}
cout << "-------------------------------\n"
<< "operator<< ๋ฅผ ์ด์ฉํ ์ถ๋ ฅ\n";
for (int i = 0; i < 3; i++)
{
cout << *p_dma[i];
}
for (int i = 0; i < 3; i++)
{
delete p_dma[i];
}
cout << "ํ๋ก๊ทธ๋จ ์ข
๋ฃ\n";
return 0;
}
4๋ฒ
//port.h
#ifndef PORT_H_
#define PORT_H_
#include<iostream>
using namespace std;
class Port
{
private:
char* brand;
char style[20];
int bottles;
public:
Port(const char* br = "none", const char* st = "none", int b = 0);
Port(const Port& p);
virtual ~Port() { delete[]brand; }
Port& operator=(const Port& p); //๊ธฐ๋ณธ ํด๋์คํ์ ์ฐธ์กฐ๊ฐ ํ์ํด๋์ค๋ฅผ ์ฐธ์กฐ๊ฐ๋ฅ ํ๊ธฐ๋๋ฌธ์ ๊ฐ์์ผ๋ก ์ ์ธํ๋ฉด ์๋๋ค.
Port& operator+=(int b); //์ฌ์ ์
Port& operator-=(int b); //์ฌ์ ์
int BottleCount() const { return bottles; }
virtual void Show() const;
friend ostream& operator<<(ostream& os, const Port& p);
};
class VintagePort :public Port
{
private:
char* nickname;
int year;
public:
VintagePort();
VintagePort(const char* br, int b, const char* nn, int y);
VintagePort(const VintagePort& vp);
~VintagePort() { delete[]nickname; }
VintagePort& operator=(const VintagePort& vp);
void Show() const;
friend ostream& operator<<(ostream& os, const VintagePort& vp);
};
#endif
//port.cpp
#include"port.h"
Port::Port(const char* br, const char* st, int b)
{
brand = new char[strlen(br) + 1];
strcpy(brand, br);
strncpy(style, st, 19);
style[19] = '\0';
bottles = b;
}
Port::Port(const Port& p)
{
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strncpy(style, p.style, 19);
style[19] = '\0';
bottles = p.bottles;
}
Port& Port::operator=(const Port& p)
{
if (this == &p)
return *this;
delete[]brand;
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strncpy(style, p.style, 19);
style[19] = '\0';
bottles = p.bottles;
return *this;
}
Port& Port::operator+=(int b) //์ฌ์ ์, b. VintagePort ํด๋์ค์ bottles๋ฅผ +=ํ๊ฒ ๋ง๋ฆ์ผ๋ก์ ์ค์๋ก ๋ค๋ฅธ ๋ณ์ ๋ณด๋ด์ง ์๊ฒํจ.
{
bottles += b;
return *this;
}
Port& Port::operator-=(int b) //์ฌ์ ์, b. VintagePort ํด๋์ค์ bottles๋ฅผ -=ํ๊ฒ ๋ง๋ฆ์ผ๋ก์ ์ค์๋ก ๋ค๋ฅธ ๋ณ์ ๋ณด๋ด์ง ์๊ฒํจ.
{
bottles -= b;
return *this;
}
void Port::Show() const
{
cout << "๋ธ๋๋: " << brand << endl
<< "์คํ์ผ: " << style << endl
<< "์๋: " << bottles << endl;
}
ostream& operator<<(ostream& os, const Port& p)
{
os << p.brand << ", " << p.style << ", " << p.bottles;
return os;
}
VintagePort::VintagePort() : Port("none","vintage",0)
{
nickname = new char[1];
nickname[0] = '\0';
year = 0;
}
VintagePort::VintagePort(const char* br, int b, const char* nn, int y) : Port(br, "vintage", b)
{
nickname = new char[strlen(nn) + 1];
strcpy(nickname, nn);
year = y;
}
VintagePort::VintagePort(const VintagePort& vp) :Port(vp)
{
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
}
VintagePort& VintagePort::operator=(const VintagePort& vp)
{
if (this == &vp)
return *this;
Port::operator=(vp);
delete[]nickname;
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
return *this;
}
void VintagePort::Show() const
{
Port::Show();
cout << "๋ณ๋ช
: " << nickname << endl
<< "์ํ ๋
๋: " << year << endl;
}
ostream& operator<<(ostream& os, const VintagePort& vp)
{
os << (const Port&)vp << ", " << vp.nickname << ", " << vp.year;
return os;
}
b. Show()๋ ๋ ํด๋์ค์์ ๋ค๋ฅธ ๋ฒ์ ์ด ์ฌ์ฉ๋๋ฏ๋ก ์ฌ์ ์ ํด์ผํจ,
๋๋จธ์ง๋ ๋ ํด๋์ค์์ ๋์ผํ๋ฏ๋ก ๋ค์ ์ ์ํ ํ์๊ฐ ์๋ค.
c. operator()๊ณผ operator<<()๋ ๊ฐ์ฒด์ ๋ฐ๋ผ ํธ์ถ๋๋ ๋ฒ์ ์ด ๋ค๋ฅด๋ฏ๋ก ๊ฐ์์ด ์๋๋ค.
728x90