Array of objects.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Writing a Good Program 6. Pointers and Arrays
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
11 Introduction to Object Oriented Programming (Continued) Cats II.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
OOP, Virtual Functions and Inheritance
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Object-Oriented Programming in C++ More examples of Association.
Fundamentals of C++ Yingcai Xiao 09/03/08. Outline Class Definition IO Template vector C Pointer Dynamic Memory Allocation.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
11 Introduction to Object Oriented Programming (Continued) Cats.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
A First Book of C++ Chapter 12 Extending Your Classes.
Chapter 8 Exercises.
Pointers What is the data type of pointer variables?
C ++ MULTIPLE CHOICE QUESTION
Standard Version of Starting Out with C++, 4th Edition
By Muhammad Waris Zargar
Programming with ANSI C ++
CSC241: Object Oriented Programming
C ++ Inheritance.
Polymorphism &Virtual Functions
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 5 Classes.
Arrays Part-1 Armen Keshishian.
Pointers and Pointer-Based Strings
Multi-dimensional Array
CSC241: Object Oriented Programming
Pointers Psst… over there.
PRG 218 Week 5 Individual Assignment Coding: Derived Classes Please click here to buy ( (
Polymorphism Lec
Lecture 8 – 9 Arrays with in a class
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Extra.
CS1201: Programming Language 2
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Pointers & Functions.
Classes Short Review of Topics already covered Constructor
Programming II Polymorphism A.AlOsaimi.
Array of objects.
Jeff West - Quiz Section 4
CS1201: Programming Language 2
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
CHAPTER 2 Arrays and Vectors.
Pointers and Pointer-Based Strings
Dynamic Memory.
CHAPTER 2 Arrays and Vectors.
CS410 – Software Engineering Lecture #5: C++ Basics III
Pointers and dynamic objects
Pointers & Functions.
Standard Version of Starting Out with C++, 4th Edition
CS 144 Advanced C++ Programming February 12 Class Meeting
Objects as Function Arguments
File I/O in C++ I.
Presentation transcript:

Array of objects

Define function create() to create array dynamically with size as global variables

Write the definition of an array called par1 of 3 objects of class person .

Write the definition of an array of 3 pointers called par1 to objects of class person.

Imagine a publishing company that markets both book and audio cassette versions of its works. Create a class publication that stores the title and the price of a publication. From this class derive two classes: book, which adds a page count tape, which adds a playing time in minutes Each of these three classes should have getdata() function to get its data from the user at the keyboard (no arguments passes ask the user inside the function ) . putdata() function to display its data.

Write a main() program that creates an array of pointers to publication. In a loop, ask the user for data about a particular book or tape, and use new to create an object of type book or tape to hold the data. When the user has finished entering the data for all books and tapes, display the resulting data for all the books and tapes entered, using a for loop and a single statement such as pubarr[j]->putdata(); to display the data from each object in the array.

Extra Examples Self read

Trace the program. #include <iostream> using namespace std ; //////////////////////////////////////// class Pet { public: Pet () {} // Constructors, Destructor ~Pet() {} void speak(){cout << "Growl" << endl;} }; ///////////////////////////////////////// class Rat: public Pet // Constructors, Destructor Rat() {} ~Rat() {} void speak(){cout << "Rat noise" << endl;}

Trace the program. class Cat: public Pet { public: Cat() {} ~Cat() {} void speak(){cout << "Meow" << endl;} }; /////////////////////////////////////////////// /////////// void chorus(Pet pt, Pet *petPtr, Pet &petRef) pt.speak(); petPtr->speak(); petRef.speak(); }

Trace the program. void main() { Pet *ptr; //Pointer to base class ptr = new Pet; cout << "Pet Created" << endl; cout << "Pets singing" << endl; chorus(*ptr,ptr,*ptr); cout << endl << endl; delete ptr; /////////////////////////////////////// ptr = new Rat; cout << "Rat Created" << endl; cout << "Rats singing" << endl; ////////////////////////////////////////// ptr = new Cat; cout << "Cat Created" << endl; cout << "Cats singing" << endl; }

Convert Speak() function to virtual function then trace the program again. class Pet { public: Pet () {} // Constructors, Destructor ~Pet() {} virtual void speak(){cout << "Growl" << endl;} };

Change class pet to be an abstract pet. { public: Pet () {} // Constructors, Destructor ~Pet() {} virtual void speak()=0; };

void main() { Pet. ptr; //Pointer to base class / void main() { Pet *ptr; //Pointer to base class /* ptr = new Pet; cout << "Pet Created" << endl; cout << "Pets singing" << endl; chorus(ptr,*ptr); cout << endl << endl; delete ptr; */ /////////////////////////////////////// ptr = new Rat; cout << "Rat Created" << endl; cout << "Rats singing" << endl; delete ptr; ////////////////////////////////////////// ptr = new Cat; cout << "Cat Created" << endl; cout << "Cats singing" << endl; }

Polymorphism allows one to declare a pointer to an object and allow it to not only point to the object the pointer was declared to point to but also point to any object that was inherited from that class. For example