adding info about memory size for virtual functions

This commit is contained in:
Jidong Xiao
2025-04-15 08:13:36 -04:00
committed by JamesFlare1212
parent 63ddc1cc22
commit 8102db4dac
3 changed files with 223 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
#include <iostream>
class Human {
};
class Student {
int age;
};
class CollegeStudent {
int age;
void print(){
std::cout << "I am a college student." << std::endl;
}
};
class CSStudent {
int age;
virtual void print(){
std::cout << "I am a CS student." << std::endl;
}
virtual void print2(){
std::cout << "I am still a CS student." << std::endl;
}
virtual void print3(){
std::cout << "I am still a CS student." << std::endl;
}
};
int main(){
CSStudent cs;
std::cout << "memory size of Human class is: " << sizeof(Human) << std::endl;
std::cout << "memory size of Student class is: " << sizeof(Student) << std::endl;
std::cout << "memory size of College Student class is: " << sizeof(CollegeStudent) << std::endl;
std::cout << "memory size of CS Student class is: " << sizeof(CSStudent) << std::endl;
return 0;
}