Files
CSCI-1200/lectures/26_inheritance_II/diamond_test1.cpp
2025-04-15 22:10:48 -04:00

30 lines
717 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
class Human {
public:
Human() { std::cout << "Human constructor\n"; }
};
class Student : public Human {
public:
Student() { std::cout << "Student constructor\n"; }
};
class Worker : public Human {
public:
Worker() { std::cout << "Worker constructor\n"; }
};
class CSStudent : public Student, public Worker {
public:
CSStudent() { std::cout << "CSStudent constructor\n"; }
};
// problem with this program: Human constructor runs twice!
int main() {
// CSStudent has two copies of Human, one via Student and one via Worker.
// Ambiguity: If we try to access a Human member from CSStudent, the compiler doesnt know which one we mean.
CSStudent cs;
return 0;
}