기록은 기억을 이기고 시간보다 오래 남는다.

프로그래밍 언어/c++

[C/C++] c++ espresso(에스프레소) chapter 08 상속 연습문제

준_준 2022. 6. 11. 17:37

1. 클래스 각각 헤더에 정의를 cpp에 내용을 적었습니다.

#pragma once
#include<iostream>
using namespace std;
class Employee
{
protected:
	string name;
	int number;
public:
	Employee();
	~Employee();
	void setEmployee(string n, int num);
	void getEmployee();
	virtual void computeSalary();
};
#include"Employee.h"
Employee::Employee()
{
	this->name = "";
	this->number = 0;
}
Employee::~Employee()
{}
void Employee::setEmployee(string n, int num)
{
	this->name = n;
	this->number = num;
}
void Employee::getEmployee()
{
	cout << "Name is: " << name << endl;
	cout << "Number is: " << number << endl;
}
void Employee::computeSalary()
{

}
#include"Employee.h"
#pragma once
class SalariedEmployee : public Employee
{private:
	int salary;
public:
	SalariedEmployee();
	~SalariedEmployee();
	void coumputeSalary();
	void setSalariedEmployee(string nam,int nu,int salar);
	void getSalariedEmployee();
};
#include"SalariedEmployee.h"
#include"Employee.h"
SalariedEmployee::SalariedEmployee()
{
	salary = 0;
}
SalariedEmployee::~SalariedEmployee()
{}
void SalariedEmployee::coumputeSalary()
{
}
 void SalariedEmployee::setSalariedEmployee(string nam, int nu, int salar)
{
	this->name = nam;
	this->salary = nu;
	this->number = salar;
}
void SalariedEmployee::getSalariedEmployee()
{
	cout << "Name is: " << name << endl;
	cout << "Number is: " << number << endl;
	cout << "Salary is: " << this->salary << endl;
}
#pragma once
#include"Employee.h"
class HourlyEmployee : public Employee
{
private:
	int hourPerMoney;
	int workTime;
public:
	HourlyEmployee();
	~HourlyEmployee();
	void setHourlyEmployee(string n, int num, int ph, int wt);
	void getHourlyEmployee();
	void computeSalary();
};
#include"HourlyEmployee.h"

HourlyEmployee::HourlyEmployee()
{
	hourPerMoney = 0;
workTime = 0;
}
HourlyEmployee::~HourlyEmployee()
{}
void HourlyEmployee::setHourlyEmployee(string n, int num,int ph,int wt)
{
	this->name = n;
	this->number = num;
	this->hourPerMoney = ph;
	this->workTime = wt;
}
void HourlyEmployee::getHourlyEmployee()
{
	cout << "Name is: " << name << endl;
	cout << "Number is: " << number << endl;
	cout << "hourPerMoney time is: " << this->hourPerMoney << endl;
	cout << "Working time is: " << this->workTime << endl;
}
void HourlyEmployee::computeSalary()
{
}

2.

#pragma once
#include<iostream>
using namespace std;
class Person
{
protected:
	string Name;
	string Address;
	int Number;
public:
	Person();
	~Person();
	void setPerson(string nama, string address, int num);
	void getPerson();
};
#include"Person.h"
Person::Person()
{
	Name="";
	Address="";
	Number = 0;
}
Person::~Person()
{}
void Person::setPerson(string name, string address, int num)
{
	this->Name = name;
	this->Address = address;
	this->Number = num;
}
void Person::getPerson()
{
	cout << "Name is: " << Name << endl;
	cout << "Address is: " << Address << endl;
	cout << "Number is: " << Number << endl;
}
#include"Person.h"
#pragma once
class Customer :public Person
{
private:
	int customerNum;
	int Milizy;
public:
	Customer();
	~Customer();
	void getCustomer();
	void setCustomer(string name, string address, int num, int customernum, int milizy);
};
#include"Customer.h"
Customer::Customer()
{
	this->customerNum=0;
	this->Milizy=0;
}
Customer::~Customer()
{}
void Customer::setCustomer(string name, string address, int num, int customernum, int milizy)
{
	this->Name = name;
	this->Address = address;
	this->Number = num;
	this->customerNum = customernum;
	this->Milizy = milizy;
}
void Customer::getCustomer()
{
	cout << "Name is: " << Name << endl;
	cout << "Address is: " << Address << endl;
	cout << "Number is: " << Number << endl;
	cout << "customerNum is: " << customerNum << endl;
	cout << "Milizy is: " << Milizy <<"점"<< endl;
}
#include"Person.h"
#include"Customer.h"

int main()
{
	Customer customer;
	customer.setCustomer("jone", "jochiwon", 010111112, 7777, 7000);
	customer.getCustomer();
	return 0;
}



3.

#pragma once
#include<iostream>
using namespace std;
class Book
{
protected:
	string Title;
	int Page;
	string madeBy;
public:
	Book();
	~Book();
};
#include"Book.h"

Book::Book()
{
	this->Title = "";
	this->Page = 0;
	this->madeBy = "";
}
Book::~Book()
{}
#pragma once
#include"Book.h"
class Magazine : public Book
{
private:
	int madeWhen;
public:
	Magazine();
	~Magazine();
	void setMagazine(string title,	int page,string madeby,int madewhen);
	void getMagazine();
};
#include"Magazine.h"
Magazine::Magazine()
{
	madeWhen = 0;
}
Magazine::~Magazine()
{
}
void Magazine::setMagazine(string title, int page, string madeby, int madewhen)
{
	this->Title = title;
	this->Page =page;
	this->madeBy = madeby;
	this->madeWhen = madewhen;

}
void Magazine::getMagazine()
{
	cout << "Title: " << Title << endl;
	cout << "Page: " << Page << endl;
	cout << "madeBy: " << madeBy << endl;
	cout << "madeWhen: " << madeWhen << endl;
}
#include"Book.h"
#include"Magazine.h"
int main()
{
	Magazine maxim;
	maxim.setMagazine("month 10", 130, "june", 20171103);
	maxim.getMagazine();
	return 0;
}

4. 일반적인 음식을 나타내는 food 클래스를 상속받아 멜론을 나타내는 Melon 클래스를 작성하여 보자. food 클래스는 칼로리, 가격, 중량, 등의 정보를 가진다. melon 클래스는 추가로 경작 농원 정보를 가진다. 생성자를 포함하여 구현해 보자.

#pragma once
#include<iostream>
using namespace std;
class Food
{protected:
	int Kalory;
	int Price;
	int Weight;
};
#pragma once
#include"Food.h"
class Melon : public Food
{
private:
	string Information;
public:
	Melon();
	~Melon();
};
#include"Melon.h"
Melon::Melon()
{
	Information = "";
}
Melon::~Melon()
{}

5. 간단하여 생략

==============================

6.

2차원 도형들을 나타내는 클래스들을 작성하여 보자. 부모 클래스인 Shape에는 도형의 위치, 크기 등의 정보가 들어간다. 각각의 멤버 변수에 대하여 접근자와 설정자 함수를 정의하라. 추가로 도형의 둘레, 면적을 계산하는 멤버 함수 getArea()도 제공해야 한다. 각 도형은 Shape에서 상속 받아서 특정 도형에 맞도록 둘레와 면적을 계산하는 getArea() 함수를 재정의 한다. 여러 도형들을 생성하고 테스트하여라.

#pragma once
#include<iostream>
using namespace std;
class Shape
{
protected:
	int X;
	int Y;
	int Size;
public:
	void setX(int x);
	void setY(int y);
	void setSize(int size);
	int getX();
	int getY();
	int getSize();
	void getArea();

};
#include"Shape.h"

void Shape::setX(int x)
{
	this->X = x;
}
void Shape::setY(int y)
{
	this->Y = y;
}
void Shape::setSize(int size)
{
	this->Size = size;
}
int Shape::getX()
{
	return X;
}
int Shape::getY()
{
	return Y;
}
int Shape::getSize()
{
	return Size;
}
void Shape::getArea()
{
}
#pragma once
#include"Shape.h"
class Rectangle : public Shape
{
public:
	void  getArea();
};

#include"Rectangle.h"

void Rectangle::getArea()
{
	int area = getX() * getY();
	cout << area << endl;
}
#pragma once
#include"Shape.h"
class Triagle : public Shape
{public:
	void getArea();
};
#include"Triagle.h"
void Triagle::getArea()
{
	int area = (getX() * getY()) / 2;
	cout << area << endl;
}
#include"Rectangle.h"
#include"Shape.h"
#include"Triagle.h"
int main()
{
	Rectangle rec1;
	rec1.setX(10);
	rec1.setY(20);
	rec1.getArea();

	Triagle tri1;
	tri1.setX(10);
	tri1.setY(20);
	tri1.getArea();
	return 0;
}

7.

#pragma once
#include<iostream>	
using namespace std;

class Student
{
protected:
	string Name;
	int studentNum;
	string Major;
	int Grade;
	int Credit;
public:
	Student();
	~Student();
	void setStudent(string name, int studentnum, string major, int grade, int credit);
	void getStudent();
};

#include"Student.h"
Student::Student()
{
	studentNum=0;
	 Grade = 0;
	 Credit = 0;
}
Student::~Student()
{}
void Student::setStudent(string name, int studentnum, string major, int grade, int credit)
{
	 this->Name=name;
	 studentNum=studentnum;
	 Major= major;
	 Grade=grade;
	 Credit=credit;
}
void Student::getStudent()
{
	cout << "Name: " << Name << endl;
	cout << "studentNum: " << studentNum << endl;
	cout << "Major: " << Major << endl;
	cout << "Grade: " << Grade << endl;
	cout << "Credit: " << Credit << endl;
}
#include"underGraduate.h"
void underGraduate::setunderGraduate(string name, int studentnum, string major, int grade, int credit, string nameofclub)
{
	this->Name = name;
	studentNum = studentnum;
	Major = major;
	Grade = grade;
	Credit = credit;
	this->nameOfClub = nameofclub;
}

void underGraduate::getunderGraduate()
{
	cout << "Name: " << Name << endl;
	cout << "studentNum: " << studentNum << endl;
	cout << "Major: " << Major << endl;
	cout << "Grade: " << Grade << endl;
	cout << "Credit: " << Credit << endl;
	cout << "Club name: " << nameOfClub << endl;
}
#pragma once
#include"Student.h"
class underGraduate :public Student
{private:
	string nameOfClub;
public:
	void setunderGraduate(string name, int studentnum, string major, int grade, int credit, string nameofclub);
	void getunderGraduate();

};
#pragma once
#include"Student.h"
class eduGraduate :public Student
{
private:
	string typeOfGraduate;
	float Money;
public:
	void setEduGraduate(string name, int studentnum, string major, int grade, int credit, string typeofgraduate, float money);
	void getEduGraduate();
};
#include"eduGraduate.h"
void eduGraduate::setEduGraduate(string name, int studentnum, string major, int grade, int credit, string typeofgraduate, float money)
{
	this->Name = name;
	studentNum = studentnum;
	Major = major;
	Grade = grade;
	Credit = credit;
	typeOfGraduate = typeofgraduate;
	Money = money;
}
void eduGraduate::getEduGraduate()
{
	cout << "Name: " << Name << endl;
	cout << "studentNum: " << studentNum << endl;
	cout << "Major: " << Major << endl;
	cout << "Grade: " << Grade << endl;
	cout << "Credit: " << Credit << endl;
	cout << "typeOfGraduate: " << typeOfGraduate << endl;
	cout << "Money: " << Money << endl;

}
#include"Student.h"
#include"underGraduate.h"
#include"eduGraduate.h"
#include<iostream>
using namespace std;
int main()
{
	Student Student1;
	Student1.setStudent("Jone", 20171103, "ManagementOFBusiness", 4, 150);
	Student1.getStudent();

	underGraduate Graduate1;
	Graduate1.setunderGraduate("JJ", 1103, "Computer Science", 1, 23, "O2CUBE");
	Graduate1.getunderGraduate();

	eduGraduate eduGraduate1;
	eduGraduate1.setEduGraduate("gg", 97777, "AI", 2, 5, "eduGraduate", 0.44);
	eduGraduate1.getEduGraduate();

	return 0;
}
반응형