CS 4303 Programming Language Concepts Lab – Chapter 5 This lab is an exercise of
ID: 3733589 • Letter: C
Question
CS 4303 Programming Language Concepts Lab –
Chapter 5
This lab is an exercise of using Java’s and C++’s dynamic binding features.
The uncompleted programs to run in Java and in C++ are given below. You can download jGrasp IDE (www.jGrasp.org) to run the Java program. Note that you need to create a driver class with the main method to run the Java program. Also, you need to write the main function to run the C++ program. Perform the following activities:
1. predict the output of the Java program.
2. run the Java program and compare your prediction to the actual output of the program.
3. Complete the C++ program such that it prints the same output as the Java program does. You need to use the given classes to generate the desired output.
4. Turn in your Java and C++ program files (.java and .cpp files) in Blackboard Learn.
The Java Program is:
class A
{ public void p()
{ System.out.println(“A.p”); }
public void q()
{ System.out.println(“A.q”); }
public void r()
{ p(); q(); }
}
class B extends A
{ public void p()
{ System.out.println(“B.p”); }
}
class C extends B
{ public void q()
{ System.out.println(“C.q”); }
public void r()
{ p(); q(); }
}
…
A a;
C c = new C();
a = c;
a.r();
a = new B();
a.r();
a = new C();
a.r();
The C++ Program is:
class A
{ public:
virtual void p()
{ cout << “A.p” << endl; }
void q()
{ cout << “A.q” << endl; }
virtual void r()
{ p(); q(); }
};
class B : public A
{ public:
void p()
{ cout << “B.p” << endl; }
};
class C : public B
{ public:
void q()
{ cout << “C.q” << endl; }
void r()
{ p(); q(); }
};
Explanation / Answer
1. java output prediction:
r() executes p() and q() from class C object.
B.p // p() is not present in class C, so p() of class B will execute as C extends B
C.q // q() is present in C. So q() of class C will execute
B.p // a is assigned object of class B. so r() will execute p() first . p() is present in class B. So B.p will be the result
A.q // q() is not present in class B. So q() will execute from class A as B extends A
B.p // r() of object of class C will result in p() of class B as p() is not present in class C
C.q // q() is present in class C . so it will execute.
2.
B.p
C.q
B.p
A.q
B.p
C.q
The output and prediction are same.
3.
4.
Java file
class A
{ public void p()
{ System.out.println("A.p"); }
public void q()
{ System.out.println("A.q"); }
public void r()
{ p(); q(); }
}
class B extends A
{ public void p()
{ System.out.println("B.p"); }
}
class C extends B
{ public void q()
{ System.out.println("C.q"); }
public void r()
{ p(); q(); }
}
class Test
{
public static void main (String[] args)
{
A a;
C c = new C();
a = c;
a.r();
a = new B();
a.r();
a = new C();
a.r();
}
}
c++ file
#include <iostream>
using namespace std;
class A
{ public:
virtual void p() // declare p() as virtual method that is override in derived classes
{ cout<<" A.p"; }
virtual void q() // declare q() as virtual method that is override in derived classes
{ cout<<" A.q"; }
void r()
{ p(); q(); }
};
class B : public A
{ public:
void p()
{ cout<<" B.p"; }
};
class C : public B
{ public:
void q()
{ cout<<" C.q"; }
void r()
{ p(); q(); }
};
int main() {
A *a = new C(); // base class pointer is used to create object of derived class
a->r();
a = new B();
a->r();
a = new C();
a->r();
return 0;
}
output:
B.p
C.q
B.p
A.q
B.p
C.q
Do ask if any query. Please upvote if the answer is useful.