티스토리 뷰

class Child : public Mother // derived class
{
};

int main()
{
	Mother mother;
	mother.setValue(1024);
	cout << mother.getValue() << endl;
	
	Child child;
	child.setValue(122);
	cout << child.getValue() << endl;
}

// 1024
// 122

 

child 클래스가 mother 클래스에 있는 것들을 사용가능.

 

#include<iostream>

using namespace std;

class Mother
{
	// protected를 사용하면 private를 유지하면서 자식들한테는 허용!
	public:
	int m_i;
	
	public:
	Mother(const int& i_in)
		 : m_i(i_in)
	{
		cout << "Mother Constructor" << endl;
	}
	
	void setValue(const int& i_in)
	{
		m_i = i_in;
	}
	
	int getValue()
	{
		return m_i;
	}
};

// 이름이 똑같은 setValue, getValue를 구현했을시, 자기 클래스에 있는것을 우선
class Child : public Mother
{
	private:
	double m_d;
	
	public:
	Child(const int & i_in, const double & d_in)
	//	: m_i(i_in), m_d(d_in);
		: Mother(i_in), m_d(d_in)
	{
	}
	
	void setValue(const int & i_in, const double & d_in)
	{
		Mother::setValue(i_in);
		m_d = d_in;
	}
	
	void setValue(const double & d_in)
	{
		m_d = d_in;
	}
	
	double getValue()
	{
		return m_d;
	}
};

int main()
{
	Mother mother(1024);
	// mother.setValue(1024);
	cout << mother.getValue() << endl;
	
	Child child(1024, 128);
	// child.Mother::setValue(1024);
	// child.setValue(122);
	cout << child.getValue() << endl;
	cout << child.Mother::getValue() << endl;
}

// Mother Constructor
// 1024
// Mother Constructor
// 128
// 1024

 

Person.h

#pragma once

#include<iostream>
#include <string>

class Person
{
	private:
	std::string m_name;
	
	public:
	// 1.
	// Person()
	// 	: m_name("No Name")
	// {}
	
	// 2.
	Person(const std::string & name_in = "No Name")
		: m_name(name_in)
	{}
	
	void setName(const std::string & name_in)
	{
		m_name = name_in;
	}
	
	std::string getName() const
	{
		return m_name;
	}
	
	void doNothing() const
	{
		std::cout << m_name << " is doing nothing " << std::endl;
	}
};

 

Teacher.h

#pragma once

// #include <string>
#include "Person.h"

class Teacher : public Person
{
	private:
	///TODO: more members like home address, ...
	
	public:
	Teacher(const std::string & name_in = "No name")
		: Person(name_in)
	{}
	
	void teach()
	{
		std::cout << getName() << " is teaching " << std::endl;
	}
	
	friend std::ostream & operator << (std::ostream & out, const Teacher & teacher)
	{
		out <<teacher.getName();
		return out;
	}
};

 

Student.h

#pragma once

#include "Person.h"

class Student : public Person
{
	private:
	int m_intel; // intelligence
	
	public:
	Student(const std::string & name_in = "No name", const int & intel_in = 0)
		// : m_name(name_in), m_intel(intel_in)
		: Person(name_in), m_intel(intel_in)
	{}
	
	void setIntel(const int & intel_in)
	{
		m_intel = intel_in;
	}
	
	int getIntel()
	{
		return m_intel;
	}
	
	void study()
	{
		std::cout << getName() << " is studying " << std::endl;
	}
	
	friend std::ostream & operator << (std::ostream & out, const Student & student)
	{
		out << student.getName();
		return out;
	}
};

 

 

school.cpp

#include "Student.h"
#include "Teacher.h"

int main()
{
	Student std("Jack Jack");
	std.setName("Jack Jack 2");
	std::cout << std.getName() << std::endl;
	
	Teacher teacher1("Dr. H");
	teacher1.setName("Dr. K");
	
	std::cout << teacher1.getName() << std::endl;
	
	std::cout << std << std::endl;
	std::cout << teacher1 << std::endl;
	
	std.doNothing();
	teacher1.doNothing();
	
	std.study();
	teacher1.teach();
	
	// 자식클래스에 구현된걸 부모클래스에는 가져다 못쓴다!
	
	return 0;
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함