예시 코드)
https://github.com/GbLeem/CPP_Study/tree/main/NoCode
30.cpp ~ 38.cpp
class A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
#include<iostream>
class Animal
{
public:
Animal()
{
std::cout << "animal constructor" << std::endl;
}
virtual ~Animal() //virtual function
{
std::cout << "animal destructor" << std::endl;
}
};
class Cat : public Animal
{
public:
Cat()
{
std::cout << "cat constructor" << std::endl;
}
~Cat()
{
std::cout << "cat destructor" << std::endl;
}
};
int main()
{
Animal* polyCat = new Cat();
delete polyCat; //base 클래스의 소멸자를 virtual로 만들지 않았을 때 cat destructor가 호출되지 않음
return 0;
}
#include<array>
#include<iostream>
class Animal
{
public:
virtual void speak()
{
std::cout << "Animal" << std::endl;
}
virtual ~Animal() = default;
};
class Cat : public Animal
{
public:
void speak() override
{
std::cout << "Cat" << std::endl;
}
};
class Dog :public Animal
{
void speak() override
{
std::cout << "Dog" << std::endl;
}
};
int main()
{
std::array<Animal*, 5> animals;
for (auto& animalPtr : animals)
{
int i = 0;
std::cin >> i;
if (i == 1)
animalPtr = new Cat();
else
animalPtr = new Dog();
}
for (auto& animalPtr : animals)
{
animalPtr->speak();
delete animalPtr;
}
return 0;
}
#include<array>
#include<iostream>
class Animal
{
public:
virtual void speak()
{
std::cout << "Animal" << std::endl;
}
private:
double height; //8byte
};
class Cat : public Animal
{
public:
void speak() override
{
std::cout << "Cat" << std::endl;
}
private:
double weight; //8+8 byte
};
int main()
{
std::cout << sizeof(Animal) << std::endl; //16 byte
std::cout << sizeof(Cat) << std::endl; //24 byte
Animal* polyAnimal = new Cat();
polyAnimal->speak(); //Cat 출력
delete polyAnimal;
return 0;
}
virtual void speak() = 0;
#include<iostream>
class Animal //abstract class
{
public:
virtual void speak() = 0; //pure virtal function
virtual ~Animal() = default;
private:
double height;
};
class Cat : public Animal
{
public:
void speak() override //override 필수!
{
std::cout << "Cat" << std::endl;
}
private:
double weight;
};
int main()
{
Animal abs; //error (abstract class는 오브젝트 생성 x)
Cat kitty;
return 0;
}
#include<iostream>
class Lion
{
public:
Lion()
{
std::cout << "Lion constructor" << std::endl;
}
virtual ~Lion()
{
std::cout << "Lion destructor" << std::endl;
}
virtual void speak()
{
std::cout << "Lion" << std::endl;
}
private:
double LionData;
};
class Tiger
{
public:
Tiger()
{
std::cout << "Tiger constructor" << std::endl;
}
virtual ~Tiger()
{
std::cout << "Tiger destructor" << std::endl;
}
virtual void speak()
{
std::cout << "Tiger" << std::endl;
}
private:
double TigerData;
};
class Liger : public Lion, public Tiger
{
public:
Liger()
{
std::cout << "Liger constructor" << std::endl;
}
~Liger()
{
std::cout << "Liger destructor" << std::endl;
}
void speak()override
{
std::cout << "Liger" << std::endl;
}
private:
double LigerData;
};
int main()
{
std::cout<< sizeof(Liger) << std::endl; // 40
Lion* polylion = new Liger();
polylion->speak(); //Liger
delete polylion;
return 0;
}
class Tiger : virtual public Animal
) 해당 문제를 해결할 수 있다.#include<iostream>
class Animal
{
public:
virtual void speak()
{
std::cout << "Animal" << std::endl;
}
private:
double animalData;
};
class Lion :virtual public Animal
{
public:
virtual void speak()
{
std::cout << "Lion" << std::endl;
}
private:
double lionData;
};
int main()
{
std::cout << sizeof(Lion) << std::endl; //32
Animal* polyAnimal = new Lion();
polyAnimal->speak();
delete polyAnimal;
return 0;
}
Animal* animalPtr = catPtr;
Animal* animalPtr = static_cast<Animal*> (catPtr);
Animal* animalDownPtr = new Cat();
Cat* catPtr = static_cast<Cat*>(animalDownPtr);
class Animal
{...};
class Cat : public Animal
{...};
class Dog : public Animal
{...};
int main()
{
Animal* animalPtr = new Animal();
Cat* catptr = dynamic_cast<Cat*>(animalPtr); //dynamic cast
if (catptr == nullptr)
{
std::cout << "not a cat object" << std::endl;
return 0;
}
catptr->speak();
catptr->knead();
return 0;
}
Animal& operator=(Animal other) = delete;
Animal(const Animal& other) = delete;
protected:
Animal(const Animal& other) = default;
3) Base 클래스를 "pure abstract class" 로 만들어서 문제 발생의 여지를 막기=> 가장 좋은 해결 방안은 base class를 pure abstract class로 만드는 것