본문 바로가기

과제모음

[C++]Inheritance(상속2)

반응형

===========================================================>>Date.h<<

#ifndef _DATE_H_
#define _DATE_H_

class Date
{
public:
 Date(int _y, int _m, int _d);
 int Year()const;
 int Month()const;
 int Day()const;
protected: 
private:
 int _year;
 int _month;
 int _day;
};
#endif

===========================================================>>Product.h<<

#ifndef _PRODUCT_H_
#define _PRODUCT_H_
#include "Date.h"
class Product:public Date
{
public:
 Product(char *N, char *M, int pro, int YY, int MM, int DD);
 ~Product();
 void OutProduct()const;
 
protected:
private:
 char *name;
 char *make;
 int price;
};
#endif

===========================================================>>main.cpp<<

#include <iostream>
#include "Product.h"
//#include "Date.h"
int main()
{
 Product p("새우깡", "농심", 700, 2005, 3, 4);
 p.OutProduct();
 return 0;
}

===========================================================>>Date.cpp<<

#include <iostream>
#include "Date.h"

Date::Date(int _y, int _m, int _d)
{
 _year = _y;
 _month = _m;
 _day = _d;
}
int Date::Year()const
{
 return _year;
}
int Date::Month()const
{
 return _month;
}
int Date::Day()const
{
 return _day;
}

===========================================================>>Product.cpp<<

#include <iostream>
#include "Product.h"
//#include "Date.h"
#include <string.h>
using std::cout;

Product::Product(char *N, char *M, int pri, int YY, int MM, int DD):Date (YY, MM, DD)
{
 name = new char[strlen(N)+1];
 make = new char[strlen(M)+1];
 strcpy(name, N);
 strcpy(make, M);
 price = pri;
}
Product::~Product()
{
 delete [] name;
 delete [] make;
}
void Product::OutProduct()const
{
 cout << "제품명 : ";
 cout << name;
 cout << "\n제조사 : ";
 cout << make;
 cout << "\n가  격 : ";
 cout << price;
 cout << "\n유통기한 : ";
 cout << Year() << "년 " << Month() << "월 " << Day() << "일 까지\n";
}


반응형

'과제모음' 카테고리의 다른 글

[C++]다중상속  (0) 2010.01.22
[C++]상속3  (0) 2010.01.22
[C++]Inheritance(상속)  (0) 2010.01.22
[C++]복사생성자2  (0) 2010.01.22
[C++]복사생성자  (0) 2010.01.22