목차
1. 은행의 계좌를 나타내는 BankAcct라는 부모 클래스를 작성한다.
2. 전자 제품을 나타내는 HomeAppliance 클래스를 작성한다.
3. 본문에 등장하는 Shape라는 클래스에 추가로 move(), getArea() 함수를 모두 가상 함수로 정의하라.
4. 3번 문제에서 새로운 도형 객체를 생성하여서 반환해주는 가상 함수 createShape()을 부모 클래스 Shape안에 정의 하고 자식 클래스에서 이 가상 함수를 구현하여 보자.
5. 도형에 관한 클래스를 약간 변경하여 보자.
6. 3차원 도형들을 칠하는데 필요한 페인트의 양을 계산하는 프로그램을 작성하여 보자.
7. 마트에서 판매되는 물건을 Product로 나타내자.
8. 책을 대여해주는 업체를 위한 Book이라는 클래스를 작성한다.
9. 간단한 게임 프로그램을 다형성을 이용하여서 작성하여 보자.
2. 전자 제품을 나타내는 HomeAppliance 클래스를 작성한다.
3. 본문에 등장하는 Shape라는 클래스에 추가로 move(), getArea() 함수를 모두 가상 함수로 정의하라.
4. 3번 문제에서 새로운 도형 객체를 생성하여서 반환해주는 가상 함수 createShape()을 부모 클래스 Shape안에 정의 하고 자식 클래스에서 이 가상 함수를 구현하여 보자.
5. 도형에 관한 클래스를 약간 변경하여 보자.
6. 3차원 도형들을 칠하는데 필요한 페인트의 양을 계산하는 프로그램을 작성하여 보자.
7. 마트에서 판매되는 물건을 Product로 나타내자.
8. 책을 대여해주는 업체를 위한 Book이라는 클래스를 작성한다.
9. 간단한 게임 프로그램을 다형성을 이용하여서 작성하여 보자.
본문내용
am>
#include
using namespace std;
class Product
{
protected:
string name;
double price;
public:
Product(string n, double p)
{
name = n;
price = p;
}
void setPrice(double p)
{
price = p;
}
virtual double getPrice()
{
return price;
}
void print()
{
cout<<"상품명 : "<
cout<<"가 격 : "<
}
};
class DiscountProduct : public Product
{
private:
double discount;
public:
DiscountProduct(string n, double p, double d) : Product(n,p)
{
discount = d;
}
double getPrice()
{
return price-(price*(discount/100));
}
void print()
{
Product::print();
cout<<"할인율 : "<
}
};
int main(void)
{
Product *p1 = new Product("toothbrush",3000);
Product *p2 = new DiscountProduct("toothbrush",3000,15);
cout<getPrice()<
cout<getPrice()<
return 0;
}
8. 책을 대여해주는 업체를 위한 Book이라는 클래스를 작성한다.
#include
#include
using namespace std;
class Book
{
protected:
int number;
string title;
string author;
public:
Book(int n, string t, string a)
{
number = n;
title = t;
author = a;
}
int getNumber()
{
return number;
}
void setNumber(int n)
{
number = n;
}
string getTitle()
{
return title;
}
void setTitle(string t)
{
title = t;
}
string getAuthor()
{
return author;
}
void setAuthor(string a)
{
author = a;
}
bool equals(Book b)
{
if( number == b.getNumber() )
return true;
else
return false;
}
virtual int getLateFees(int days) = 0;
void print()
{
cout<<"관리번호 : "<
cout<<"제 목 : "<
cout<<"저 자 : "<
}
};
class Novel : public Book
{
public:
Novel(int n, string t, string a) : Book(n,t,a) {}
int getLateFees(int days)
{
return days * 300;
}
};
class Poet : public Book
{
public:
Poet(int n, string t, string a) : Book(n,t,a) {}
int getLateFees(int days)
{
return days * 200;
}
};
class ScienceFiction : public Book
{
public:
ScienceFiction(int n, string t, string a) : Book(n,t,a) {}
int getLateFees(int days)
{
return days * 600;
}
};
int main(void)
{
Book *b1 = new Novel(1111,"소설책","소설저자");
Book *b2 = new Poet(1111,"시집","시인");
Book *b3 = new ScienceFiction(1111,"공상과학책","공상저자");
b1->print();
cout<getLateFees(3)<<"원"<
b2->print();
cout<getLateFees(4)<<"원"<
b3->print();
cout<getLateFees(5)<<"원"<
return 0;
}
9. 간단한 게임 프로그램을 다형성을 이용하여서 작성하여 보자.
#include
#include
using namespace std;
class GameCharacter
{
public:
virtual void draw(){}
};
class Hobit : public GameCharacter
{
public:
Hobit() : GameCharacter(){}
void draw()
{
cout<<"호빗을 그립니다."<
}
};
class Titan : public GameCharacter
{
public:
Titan() : GameCharacter(){}
void draw()
{
cout<<"타이탄을 그립니다."<
}
};
class Magician : public GameCharacter
{
public:
Magician() : GameCharacter(){}
void draw()
{
cout<<"주술사를 그립니다."<
}
};
int main(void)
{
GameCharacter *ac[3];
ac[0] = new Hobit();
ac[1] = new Titan();
ac[2] = new Magician();
for(int i = 0; i < 3 ; i++){
cout<<"======================"<
ac[i]->draw();
cout<<"======================"<
}
return 0;
}
#include
using namespace std;
class Product
{
protected:
string name;
double price;
public:
Product(string n, double p)
{
name = n;
price = p;
}
void setPrice(double p)
{
price = p;
}
virtual double getPrice()
{
return price;
}
void print()
{
cout<<"상품명 : "<
};
class DiscountProduct : public Product
{
private:
double discount;
public:
DiscountProduct(string n, double p, double d) : Product(n,p)
{
discount = d;
}
double getPrice()
{
return price-(price*(discount/100));
}
void print()
{
Product::print();
cout<<"할인율 : "<
};
int main(void)
{
Product *p1 = new Product("toothbrush",3000);
Product *p2 = new DiscountProduct("toothbrush",3000,15);
cout<
}
8. 책을 대여해주는 업체를 위한 Book이라는 클래스를 작성한다.
#include
#include
using namespace std;
class Book
{
protected:
int number;
string title;
string author;
public:
Book(int n, string t, string a)
{
number = n;
title = t;
author = a;
}
int getNumber()
{
return number;
}
void setNumber(int n)
{
number = n;
}
string getTitle()
{
return title;
}
void setTitle(string t)
{
title = t;
}
string getAuthor()
{
return author;
}
void setAuthor(string a)
{
author = a;
}
bool equals(Book b)
{
if( number == b.getNumber() )
return true;
else
return false;
}
virtual int getLateFees(int days) = 0;
void print()
{
cout<<"관리번호 : "<
};
class Novel : public Book
{
public:
Novel(int n, string t, string a) : Book(n,t,a) {}
int getLateFees(int days)
{
return days * 300;
}
};
class Poet : public Book
{
public:
Poet(int n, string t, string a) : Book(n,t,a) {}
int getLateFees(int days)
{
return days * 200;
}
};
class ScienceFiction : public Book
{
public:
ScienceFiction(int n, string t, string a) : Book(n,t,a) {}
int getLateFees(int days)
{
return days * 600;
}
};
int main(void)
{
Book *b1 = new Novel(1111,"소설책","소설저자");
Book *b2 = new Poet(1111,"시집","시인");
Book *b3 = new ScienceFiction(1111,"공상과학책","공상저자");
b1->print();
cout<
cout<
cout<
}
9. 간단한 게임 프로그램을 다형성을 이용하여서 작성하여 보자.
#include
#include
using namespace std;
class GameCharacter
{
public:
virtual void draw(){}
};
class Hobit : public GameCharacter
{
public:
Hobit() : GameCharacter(){}
void draw()
{
cout<<"호빗을 그립니다."<
};
class Titan : public GameCharacter
{
public:
Titan() : GameCharacter(){}
void draw()
{
cout<<"타이탄을 그립니다."<
};
class Magician : public GameCharacter
{
public:
Magician() : GameCharacter(){}
void draw()
{
cout<<"주술사를 그립니다."<
};
int main(void)
{
GameCharacter *ac[3];
ac[0] = new Hobit();
ac[1] = new Titan();
ac[2] = new Magician();
for(int i = 0; i < 3 ; i++){
cout<<"======================"<
cout<<"======================"<
return 0;
}
소개글