Polymorphism Dr. Leon Jololian. Dr.Jololian2 class Person { private: string name; int age; public: Person(string na, int ag); Person(string na); string.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
Example T1: T2: Constraint: Susan’s salary = Jane’s salary
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
1 CSC241: Object Oriented Programming Lecture No 21.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
1 Chapter 11 Introducing the Class Pages ( )
ECE 264 Object-Oriented Software Development
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.
CSC241 Object-Oriented Programming (OOP) Lecture No. 18
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
CSE 114 – Computer Science I Inheritance Yosemite National Park, California.
10-Nov-15 Java Object Oriented Programming What is it?
Class Inheritance Dr. Leon Jololian. Dr.Jololian2 class Person { private: string name; int age; public: Person(string na, int ag); Person(string na);
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
SNPL1 Woochang Lim C+OOP = C++ C (non OOP)  C++ (non OOP+OOP)  Java (OOP) Object-Oriented Design  Object-Oriented Programming Programming with C++
Objective: Students will be able to: Declare and use variables Input integers.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
COMP Inheritance Basics Yi Hong June 09, 2015.
Chapter Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};
Classes and Objects - Part I. What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Exercises on Polymorphism and Operator Overloading TCP1201: 8.
CS32 Discussion Section 1B Week 5 TA: Hao Yu (Cody)
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Object-Oriented Programming (OOP) Lecture No. 24.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Arrays 2(Chapter 8.1,8.5) Solution 5 Solution 6 Multidimensional arrays Dynamic Array Problems.
OBJECT ORIENTED PROGRAMMING
Interfaces.
COMP 110 More about inheritance
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
What is polymorphism?.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
An Introduction to Java – Part II
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
CS 1430: Programming in C++.
Private.
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Object-Oriented Programming (OOP) Lecture No. 22
Continued from last class
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
CS 144 Advanced C++ Programming March 28 Class Meeting
CIS 110: Introduction to computer programming
CSE Module 1 A Programming Primer
Presentation transcript:

Polymorphism Dr. Leon Jololian

Dr.Jololian2 class Person { private: string name; int age; public: Person(string na, int ag); Person(string na); string getName(); int getAge(); void setName(string na); void setAge(int ag); void print(); };

Dr.Jololian3 class Student : public Person { private: string major; float gpa; public: Student(string na, int ag, string ma, float gp); string getMajor() { return major; } float getGpa() { return gpa; } void setMajor(string ma) { major = ma; } void setGpa(float gp) { gpa = gp; } void print(); };

Dr.Jololian4 #include "Student.h" void main() { Person per("John Doe", 18); per.print(); Student stu("Jane Smith", 19, "Business", float(3.8)); stu.print(); Person* p; p = &per; p->setAge(21); p->print(); p = &stu; p->setAge(20); p->print(); }

Dr.Jololian5 Name: John Doe Age: 18 Name: Jane Smith Age: 19 Major: Business GPA: 3.8 Name: John Doe Age: 21 Name: Jane Smith Age: 20 Output

Dr.Jololian6 class Person { private: string name; int age; public: … virtual void print(); }; class Student : public Person { private: string major; float gpa; public: … void print(); }; Virtual Function

Dr.Jololian7 Output Name: John Doe Age: 18 Name: Jane Smith Age: 19 Major: Business GPA: 3.8 Name: John Doe Age: 21 Name: Jane Smith Age: 20 Major: Business GPA: 3.8

Dr.Jololian8 class Employee : public Person { private: string title; float salary; public: Employee( string n, int a, string t, float s); //... void print(); };

Dr.Jololian9 Employee::Employee(string n, int a, string t, float s) : Person(n, a) { title = t; salary = s; } void Employee::print(){ Person::print(); cout << "Title: " << title << endl; cout << "Salary: " << salary << endl<<endl; }

Dr.Jololian10 void main() { Employee emp("Bill Johnson", 25, "Programmer", float( )); Person* p; emp.print(); p = &emp; p->print(); }

Dr.Jololian11 Output Name: Bill Johnson Age: 25 Title: Programmer Salary: Name: Bill Johnson Age: 25 Title: Programmer Salary: 54325

Dr.Jololian12 class Undergrad : public Student { private: int year; public: Undergrad(string n, int a, string m, float g, int y); void print(); };

Dr.Jololian13 Undergrad::Undergrad(string n, int a, string m, float g, int y) : Student(n, a, m, g) { year = y; } void Undergrad::print() { Student::print(); cout << "Year: " << year << endl; }

Dr.Jololian14 void main() { Person* p; Undergrad und("Tom Jones", 19, "Accounting", float(3.99), 3); und.print(); p = &und; p->print(); }

Dr.Jololian15 Output Name: Tom Jones Age: 19 Major: Accounting GPA: 3.99 Year: 3 Name: Tom Jones Age: 19 Major: Accounting GPA: 3.99 Year: 3

Dr.Jololian16