wa1 - ECE 462 Written Assignment 1 Due 8:30AM Name PUID 1 C...

Info iconThis preview shows pages 1–5. 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? #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) { } void print() { Person::print(); cout << "school: " << s_school << endl; } }; int main(int argc, char * argv[]) 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
{ 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 Java Derived Class (Outcome 3) What is the output of this program? 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; 2
Background image of page 2
} public void func2() { p_val += 4; } }; class outcome342 { 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()); } } 3 Inheritance and Class (Outcome 3) Which statement is correct? A. If a C++ class is abstract, all methods must be purely virtual. B. In Java, a private attribute can be seen by derived classes; a protected attribute cannot be seen by derived classes. C. Polymorphism means classes can have derived classes. D. The only purpose of creating a class is to encapsulate data. E. A derived class has all the public methods from the base class. 4 C++ Abstract Class (Outcome 3) How to make Shape an abstract class and Triangle must override the getArea method? 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
#include <iostream> #include <string> using namespace std; class Shape { public: Shape() { } virtual ˜Shape() { } // >>>>> // getArea must be overridden in derived classes // <<<<< }; class Triangle : public Shape { public: Triangle(double h, double w) { width = w; height = h; } virtual double getArea() { return (0.5 * height * width); } virtual ˜Triangle() { } private: double height; double width;
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/19/2012 for the course ECE 462 taught by Professor Samuelmidkiff during the Fall '11 term at Purdue.

Page1 / 16

wa1 - ECE 462 Written Assignment 1 Due 8:30AM Name PUID 1 C...

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

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