Skip to content

Commit de5ce49

Browse files
committed
Add code for Intro to C++ Object Model
1 parent df4bd6c commit de5ce49

16 files changed

+319
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <type_traits>
3+
using std::cout;
4+
5+
struct Complex
6+
{
7+
float real;
8+
float imag;
9+
};
10+
11+
int main()
12+
{
13+
cout<<"is Complex a POD? "<<
14+
(std::is_pod<Complex>() ? "yes" : "no")<<"\n";
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
3+
typedef struct
4+
{
5+
float real;
6+
float imag;
7+
} Complex;
8+
9+
int main()
10+
{
11+
Complex c;
12+
printf("address of c: %p\n", &c);
13+
printf("address of c.real: %p\n", &c.real);
14+
printf("address of c.imag: %p\n", &c.imag);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
3+
typedef struct
4+
{
5+
float real;
6+
float imag;
7+
} Complex;
8+
9+
int main()
10+
{
11+
printf("sizeof(float): %ld\n", sizeof(float));
12+
printf("sizeof(Complex): %ld\n", sizeof(Complex));
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
struct Erdos
5+
{
6+
Erdos() { whoAmIReally(); }
7+
virtual void whoAmIReally() { cout<<"I really am Erdos\n"; }
8+
};
9+
10+
struct Fermat : public Erdos
11+
{
12+
virtual void whoAmIReally() { cout<<"I really am Fermat\n"; }
13+
};
14+
15+
int main()
16+
{
17+
Erdos e;
18+
Fermat f;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
struct Complex
5+
{
6+
float real;
7+
float imag;
8+
};
9+
10+
struct Derived : public Complex
11+
{
12+
float angle;
13+
};
14+
15+
int main()
16+
{
17+
cout<<"sizeof(float): "<<sizeof(float)<<"\n";
18+
cout<<"sizeof(Complex): "<<sizeof(Complex)<<"\n";
19+
cout<<"sizeof(Derived): "<<sizeof(Derived)<<"\n";
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <cmath>
3+
using std::cout;
4+
5+
struct Complex
6+
{
7+
float Abs() const { return std::hypot(real, imag); }
8+
float real;
9+
float imag;
10+
};
11+
12+
int main()
13+
{
14+
cout<<"sizeof(float): "<<sizeof(float)<<"\n";
15+
cout<<"sizeof(Complex): "<<sizeof(Complex)<<"\n";
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
struct Complex
5+
{
6+
float real;
7+
float imag;
8+
};
9+
10+
int main()
11+
{
12+
Complex c;
13+
cout<<"address of c: "<<&c<<"\n";
14+
cout<<"address of c.real: "<<&c.real<<"\n";
15+
cout<<"address of c.imag: "<<&c.imag<<"\n";
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
struct Complex
5+
{
6+
float real;
7+
float imag;
8+
};
9+
10+
struct Derived : public Complex
11+
{
12+
float angle;
13+
};
14+
15+
int main()
16+
{
17+
Derived d;
18+
cout<<"address of d: "<<(&d)<<"\n";
19+
cout<<"address of d.real: "<<(&d.real)<<"\n";
20+
cout<<"address of d.imag: "<<(&d.imag)<<"\n";
21+
cout<<"address of d.angle: "<<(&d.angle)<<"\n";
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include <cmath>
3+
using std::cout;
4+
5+
struct Complex
6+
{
7+
virtual ~Complex() = default;
8+
virtual float Abs() { return std::hypot(real, imag); }
9+
float real;
10+
float imag;
11+
};
12+
13+
struct Derived : public Complex
14+
{
15+
virtual ~Derived() = default;
16+
virtual float Abs() { return std::hypot(std::hypot(real, imag), angle); }
17+
float angle;
18+
};
19+
20+
int main()
21+
{
22+
Derived d;
23+
cout<<"address of d: "<<(&d)<<"\n";
24+
cout<<"address of d.real: "<<(&d.real)<<"\n";
25+
cout<<"address of d.imag: "<<(&d.imag)<<"\n";
26+
cout<<"address of d.angle: "<<(&d.angle)<<"\n";
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
3+
struct Complex
4+
{
5+
float real;
6+
float imag;
7+
};
8+
9+
int main()
10+
{
11+
std::cout<<"sizeof(float): "<<sizeof(float)<<"\n";
12+
std::cout<<"sizeof(Complex): "<<sizeof(Complex)<<"\n";
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <cmath>
3+
using std::cout;
4+
5+
struct Complex
6+
{
7+
virtual ~Complex() = default;
8+
virtual float Abs() { return std::hypot(real, imag); }
9+
float real;
10+
float imag;
11+
};
12+
13+
struct Derived : public Complex
14+
{
15+
virtual ~Derived() = default;
16+
virtual float Abs() { return std::hypot(std::hypot(real, imag), angle); }
17+
float angle;
18+
};
19+
20+
int main()
21+
{
22+
cout<<"sizeof(float): "<<sizeof(float)<<"\n";
23+
cout<<"sizeof(void*): "<<sizeof(void*)<<"\n";
24+
cout<<"sizeof(Complex): "<<sizeof(Complex)<<"\n";
25+
cout<<"sizeof(Derived): "<<sizeof(Derived)<<"\n";
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
class Complex
5+
{
6+
float real;
7+
float imag;
8+
};
9+
10+
int main()
11+
{
12+
cout<<"sizeof(float): "<<sizeof(float)<<"\n";
13+
cout<<"sizeof(Complex): "<<sizeof(Complex)<<"\n";
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
struct Erdos
5+
{
6+
void whoAmI() { cout<<"I am Erdos\n"; }
7+
virtual void whoAmIReally() { cout<<"I really am Erdos\n"; }
8+
};
9+
10+
struct Fermat : public Erdos
11+
{
12+
void whoAmI() { cout<<"I am Fermat\n"; }
13+
virtual void whoAmIReally() { cout<<"I really am Fermat\n"; }
14+
};
15+
16+
int main()
17+
{
18+
Erdos e;
19+
e.whoAmI();
20+
e.whoAmIReally();
21+
22+
Fermat f;
23+
f.whoAmI();
24+
f.whoAmIReally();
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
struct Erdos
5+
{
6+
void whoAmI() { cout<<"I am Erdos\n"; }
7+
virtual void whoAmIReally() { cout<<"I really am Erdos\n"; }
8+
};
9+
10+
struct Fermat : public Erdos
11+
{
12+
void whoAmI() { cout<<"I am Fermat\n"; }
13+
virtual void whoAmIReally() { cout<<"I really am Fermat\n"; }
14+
};
15+
16+
int main()
17+
{
18+
Fermat f;
19+
f.whoAmI();
20+
f.whoAmIReally();
21+
22+
Erdos& e = f;
23+
e.whoAmI();
24+
e.whoAmIReally();
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
struct Erdos
5+
{
6+
void whoAmI() { cout<<"I am Erdos\n"; }
7+
virtual void whoAmIReally() { cout<<"I really am Erdos\n"; }
8+
};
9+
10+
struct Fermat : public Erdos
11+
{
12+
void whoAmI() { cout<<"I am Fermat\n"; }
13+
virtual void whoAmIReally() { cout<<"I really am Fermat\n"; }
14+
};
15+
16+
int main()
17+
{
18+
Erdos * e1 = new Erdos;
19+
e1->whoAmI();
20+
e1->whoAmIReally();
21+
22+
Erdos * e2 = new Fermat;
23+
e2->whoAmI();
24+
e2->whoAmIReally();
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
using std::cout;
3+
4+
struct Erdos
5+
{
6+
void whoAmI() { cout<<"I am Erdos\n"; }
7+
virtual void whoAmIReally() { cout<<"I really am Erdos\n"; }
8+
};
9+
10+
struct Fermat : public Erdos
11+
{
12+
void whoAmI() { cout<<"I am Fermat\n"; }
13+
virtual void whoAmIReally() { cout<<"I really am Fermat\n"; }
14+
};
15+
16+
int main()
17+
{
18+
using std::unique_ptr;
19+
using std::make_unique;
20+
21+
unique_ptr<Erdos> e = make_unique<Erdos>();
22+
e->whoAmI();
23+
e->whoAmIReally();
24+
25+
unique_ptr<Fermat> f = make_unique<Fermat>();
26+
f->whoAmI();
27+
f->whoAmIReally();
28+
}

0 commit comments

Comments
 (0)