wa1soln - ECE 462 Written Assignment 1 Due: 8:30AM,...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE 462 Written Assignment 1 Due: 8:30AM, September 30, 2011 Name: PUID: 1 C++ Polymorphism (Outcome 3) What is the output of this program? Answer: outcome331.cpp: In function ‘int main(int, char ** )’: outcome331.cpp:42: error: invalid conversion from ‘Person * ’ to ‘Student * This is because we are trying to assign a base class pointer to a derived class pointer. #include <iostream> #include <string> using namespace std; class Person { private: string p_name; public: Person(string n): p_name(n) { } void print() { cout << "name: " << p_name << endl; } }; class Student: public Person { private: string s_school; public: Student(string n, string sch): Person(n), s_school(sch) { } 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
void print() { Person::print(); cout << "school: " << s_school << endl; } }; int main(int argc, char * argv[]) { Person * per[3]; per[0] = new Person("John"); per[1] = new Student("Amy", "Purdue"); per[2] = new Person("Tom"); Student * stu[2]; stu[0] = new Student("Jennifer", "Indiana"); stu[1] = new Student("Charlie", "Lafayette"); per[1] = stu[1]; stu[0] = per[0]; stu[0] -> print(); return 0; } 2
Background image of page 2
2 Java Derived Class (Outcome 3) What is the output of this program? Answer: 5 7 per[1].func1 calls Student func1 per[1].func2 calls Person func2 because Student doesn’t implement a func2 to override Person’s func2. stu[1].func1 calls Student func1 stu[2].func2 calls CollegeStudent func2. import java.io. * ; import java.util. * ; class Person { private String p_name; protected int p_val; public Person(String n) { p_val = 0; p_name = n; } public void func1() { p_val ++; } public void func2() { p_val += 2; } public int getVal() { return p_val; } } class Student extends Person { private String s_school; public Student(String n, String sch) { super(n); s_school = sch; } public void func1() { p_val += 3; } } class CollegeStudent extends Student { private String cs_major; public CollegeStudent(String n, String sch, String ma) { super(n, sch); cs_major = ma; } public void func2() { p_val += 4; } }; class outcome342 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
{ public static void main(String[] args) { Person [] per = new Person[3]; per[0] = new Person("John"); per[1] = new Student("Amy", "Purdue"); per[2] = new CollegeStudent("Tom", "West Lafayette", "ECE"); Student [] stu = new Student[2]; stu[0] = new Student("Jennifer", "Indiana"); stu[1] = new CollegeStudent("Mary", "Indiana", "Math"); per[1].func1(); per[1].func2(); stu[1].func1(); stu[1].func2(); System.out.println(per[1].getVal()); System.out.println(stu[1].getVal()); } } 4
Background image of page 4
3 Inheritance and Class (Outcome 3) Which statement is correct? Answer: E A is not right because while there needs to be at least one, not all need to be B is not right because private attributes are not visible outside of the class. C is not right because while polymorphic classes can be derived from, a class is polymorphic because
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

wa1soln - ECE 462 Written Assignment 1 Due: 8:30AM,...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online