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

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

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
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?
Classes and Objects Instructor: 小黑. Files and Streams 1 #include int main( void ) { 5 char input[ 5 ]; 6 7 FILE *cfPtr; 8 9 if (( cfPtr = fopen(
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.
Polymorphism Dr. Leon Jololian. Dr.Jololian2 class Person { private: string name; int age; public: Person(string na, int ag); Person(string na); string.
CSC241 Object-Oriented Programming (OOP) Lecture No. 18
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.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
CSE 114 – Computer Science I Inheritance Yosemite National Park, California.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
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.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
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!
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.
CS32 Discussion Section 1B Week 5 TA: Hao Yu (Cody)
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
Object-Oriented Programming (OOP) Lecture No. 24.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Arrays 2(Chapter 8.1,8.5) Solution 5 Solution 6 Multidimensional arrays Dynamic Array Problems.
Andrew(amwallis) Classes!
OBJECT ORIENTED PROGRAMMING
Mark Redekopp David Kempe
Interfaces.
What is Encapsulation, Benefits, Implementation in Java
Anatomy of a class Part I
1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions
Student Data Score First Name Last Name ID GPA DOB Phone ...
CS 1430: Programming in C++.
CS 1430: Programming in C++.
An Introduction to Java – Part II
Creating Objects in a Few Simple Steps
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Counting Loops.
Object-Oriented Programming (OOP) Lecture No. 14
CS 1430: Programming in C++.
Private.
The Object-Oriented Thought Process Chapter 04
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
Anatomy of a class Part I
CIS 110: Introduction to computer programming
CSE Module 1 A Programming Primer
Presentation transcript:

Class Inheritance 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 Person::Person(string na, int ag) { name = na; age = ag; } Person::Person(string na) { name = na; } Constructors

Dr.Jololian4 “Get”-ter Functions string Person::getName() { return name; } int Person::getAge() { return age; }

Dr.Jololian5 “Set”-ter Functions void Person::setName(string na) { name = na; } void Person::setAge(int ag) { age = ag; } void Person::print() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; }

Dr.Jololian6 Main Function void main() { Person p("John Doe", 18); p.print(); Person q("Jane Smith"); q.setAge(21); q.print(); q.setName("Jane Johnson"); q.print(); }

Dr.Jololian7 Output Name: John Doe Age: 18 Name: Jane Smith Age: 21 Name: Jane Johnson Age: 21

Dr.Jololian8 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.Jololian9 Student::Student(string na, int ag, string ma, float gp) : Person(na, ag) { major = ma; gpa = gp; } void Student::print() { Person::print(); cout << "Major: " << major << endl; cout << "GPA: " << gpa << endl; }

Dr.Jololian10 Student s("Max Gomez", 19, "Business", float(3.8)); s.print(); s.setAge(20); s.setGpa(3.75); s.print(); Name: Max Gomez Age: 19 Major: Business GPA: 3.8 Name: Max Gomez Age: 20 Major: Business GPA: 3.75

Dr.Jololian11 class Employee : public Person { private: string company; float salary; public: Employee(string na, int ag, string co, float sa); string getCompany(){ return company; } float getSalary() { return salary; } void setCompany(string co) { company = co; } void setSalary(float sa) {salary = sa; } void print(); };

Dr.Jololian12 Employee::Employee(string na, int ag, string co, float sa) :Person(na, ag) { company = co; salary = sa; } void Employee::print(){ Person::print(); cout << "Company: " << company << endl; cout << "Salary: " << salary << endl; }

Dr.Jololian13 Name: Joe Brown Age: 34 Company: IBM Salary: Name: Joe Brown Age: 38 Company: IBM Salary: Employee e("Joe Brown", 34, "IBM", float( )); e.print(); e.setAge(38); e.setSalary(float( )); e.print();

Dr.Jololian14