-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectSlicing.cpp
63 lines (47 loc) · 927 Bytes
/
objectSlicing.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Last sanity check: 2020-05-18
//
// main.cpp
// Cpp
//
// Created by Alex Seceleanu on 4/9/18.
// Copyright © 2018 Alex Seceleanu. All rights reserved.
//
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Parent
{
int one;
public:
Parent (): one(0) {}
Parent(const Parent &other): one(0)
{ cout<< "Copy parent"<<endl; }
virtual void print()
{
cout << "Parent" << endl;
}
virtual ~Parent() {}
};
class Child: public Parent
{
int two;
public:
Child():two(0) {}
void print()
{
cout << "Child" << endl;
}
};
int main()
{
Child c1;
Parent &p1 = c1;
p1.print();
//This is object slicing - upcasting a child object to the parent type
//changing one type to another
Parent p2 = Child();
p2.print();
return 0;
}