Student name: Nguyễn Phúc Điền
Student ID: 2352258
Group: CC01
C++ Java
#include <iostream> class A {
protected int a;
class A
public A() {
{
a = 10;
protected: }
int a;
public: public int f() {
A() {a = 10;} a++;
int f() {a++; return a;} return a;
int geta() {return a;} }
};
public int geta() {
return a;
}
}
class B: public A class B extends A {
{ @Override
public int f() {
public:
a--;
int f() {a--; return a;} return a;
}; }
}
int main() public class Main {
public static void
{
main(String[] args) {
int n; int n;
A a,a2; A a = new A();
B b; A a2 = new A();
B b = new B();
a.f();
b.f(); a.f();
b.f();
a2 = b;
a2 = b;
a2.f(); a2.f();
std::cout << a.geta() << " System.out.println(a.geta() + "
"<< b.geta() << " " " + b.geta() + " " +
<<a2.geta(); a2.geta());
}
}
return 0;
}
Result: 11 9 10 Result: 11 8 8
Question 1: Why do they give different results?
C++:
1. “a2 = b” performs object slicing (C++ special feature). This means that when the object b
of type B is assigned to a2 of type A, the part of b that is specific to B is sliced off, keeping
only the A portion
2. Even though b's value of a was decremented to 9 before the slicing, the sliced copy a2
has no knowledge of B's behavior, and fields inherited from B are copied by value
3. When calling “a2.f()”, it invokes A::f() (not B::f()), because a2 is now just of type A.
Therefore, the result is 10 (9 + 1 = 10)
→ RESULT: 11 9 10
Java:
In Java, there is no object slicing as in C++. Java uses references rather than direct value
assignment when working with objects, so “a2 = b” does not create a sliced copy; instead, a2
refers to the same B object
a.f(); → property a of object a is 11 = 10 + 1
b.f(); → property a of object b is 9 = 10 - 1
a2 = b; → makes a2 hold a reference to the same object as b, which means a2 and b refers
to the same object (same address in memory)
a2.f(); → calls f() on the same object referenced by a2 (which is really the B object). Since
a2 points to a B object and Java resolves methods based on the runtime type of the
object (polymorphism), B::f() is invoked. Property a of a2 (or b) is 8 = 9 - 1
→ RESULT: 11 8 8
Question 2: If we want C++ to act as Java, how can?
We can make C++ to act as Java by using pointers or references.
Below is the new code using pointers:
#include <iostream>
class A {
protected:
int a;
public:
A() { a = 10; }
virtual int f() { a++; return a; } // Use virtual for
polymorphism
int geta() { return a; }
};
class B : public A {
public:
int f() { a--; return a; }
};
int main() {
A a;
A* a2; // Pointer to base class A
B b;
a.f();
b.f();
a2 = &b; // Assign address of b to a2 (no slicing, a2 points
to b)
a2->f(); // Calls B::f() because a2 points to B, b.a becomes
8
std::cout << a.geta() << " " << b.geta() << " " << a2->geta()
<< std::endl;
return 0;
}
There are 2 different key changes in the new C++ code:
1. Declare a2 as a pointer of type A so that when assigning “a2 = &b;”, we make a2
point to b (no object slicing).
2. Make A::f() a virtual method for polymorphism. Without doing this, when we call
“a2→f();”, a2 will perform the A::f() method instead of performing polymorphism.
Another way is using reference:
#include <iostream>
class A {
protected:
int a;
public:
A() { a = 10; }
virtual int f() { a++; return a; } // Use virtual for
polymorphism
int geta() { return a; }
};
class B : public A {
public:
int f() override { a--; return a; } // Override for runtime
polymorphism
};
int main() {
A a;
B b;
A& a2 = b; // Use a reference to base class A, but point it to
b
a.f();
b.f(); // B::f(), b.a becomes 9
a2.f(); // Calls B::f() because a2 refers to B, b.a becomes
8
std::cout << a.geta() << " " << b.geta() << " " << a2.geta() <<
std::endl;
return 0;
}
There are 2 different key changes in the new C++ code:
1. Declare a reference to base class A, but point it to b. Therefore, when calling a2.f(),
actually it is B::f().
NOTE: references must be initialized immediately. We cannot declare it and assign
the value later.
2. Make A::f() a virtual method for polymorphism. Without doing this, when we call
“a2.f();”, a2 will perform the A::f() method instead of performing polymorphism.