Object-Oriented Programming Concepts Illustrated

undefined
 
Array of objects
D
e
f
i
n
e
 
f
u
n
c
t
i
o
n
 
c
r
e
a
t
e
(
)
 
t
o
 
c
r
e
a
t
e
 
a
r
r
a
y
 
d
y
n
a
m
i
c
a
l
l
y
 
w
i
t
h
 
s
i
z
e
 
a
s
 
g
l
o
b
a
l
v
a
r
i
a
b
l
e
s
W
r
i
t
e
 
t
h
e
 
d
e
f
i
n
i
t
i
o
n
 
o
f
 
a
n
 
a
r
r
a
y
 
c
a
l
l
e
d
 
p
a
r
1
 
o
f
 
3
 
o
b
j
e
c
t
s
 
o
f
 
c
l
a
s
s
 
p
e
r
s
o
n
 
.
W
r
i
t
e
 
t
h
e
 
d
e
f
i
n
i
t
i
o
n
 
o
f
 
a
n
 
a
r
r
a
y
 
o
f
 
3
 
p
o
i
n
t
e
r
s
 
c
a
l
l
e
d
 
p
a
r
1
 
 
t
o
 
o
b
j
e
c
t
s
 
o
f
 
c
l
a
s
s
 
p
e
r
s
o
n
.
 
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.
 
 
 
 
 
 
 
S
e
l
f
 
r
e
a
d
 
#include
 
<iostream>
using
 
namespace
 std ;
////////////////////////////////////////
class
 Pet
{
public
:
   Pet () {}    
// Constructors, Destructor
  ~Pet() {}
 
    
void
 speak(){cout << 
"Growl"
 << endl;}
};
/////////////////////////////////////////
class
 Rat: 
public
 Pet
{
public
:
// 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;
    chorus(*ptr,ptr,*ptr);
    cout << endl << endl;
    
delete
 ptr;
//////////////////////////////////////////
    ptr = 
new
 Cat;
    cout << 
"Cat Created"
 << endl;
    cout << 
"Cats singing"
 << endl;
    chorus(*ptr,ptr,*ptr);
    cout << endl << endl;
 
    
delete
 ptr;
}
 
Trace the program
.
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.
 
class
 Pet
{
public
:
   Pet () {}    
// Constructors, Destructor
  ~Pet() {}
  
virtual
  
void
 speak()=0;
};
 
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;
    chorus(ptr,*ptr);
 
    cout << endl << endl;
 
    
delete
 ptr;
//////////////////////////////////////////
    ptr = 
new
 Cat;
    cout << 
"Cat Created"
 << endl;
    cout << 
"Cats singing"
 << endl;
    chorus(ptr,*ptr);
    cout << endl << endl;
 
    
delete
 ptr;
}
 
 
 
 
P
o
l
y
m
o
r
p
h
i
s
m
 
a
l
l
o
w
s
 
o
n
e
 
t
o
d
e
c
l
a
r
e
 
a
 
p
o
i
n
t
e
r
 
t
o
 
a
n
o
b
j
e
c
t
 
a
n
d
 
a
l
l
o
w
 
i
t
 
t
o
 
n
o
t
o
n
l
y
 
p
o
i
n
t
 
t
o
 
t
h
e
 
o
b
j
e
c
t
 
t
h
e
p
o
i
n
t
e
r
 
w
a
s
 
d
e
c
l
a
r
e
d
 
t
o
p
o
i
n
t
 
t
o
 
b
u
t
 
a
l
s
o
 
p
o
i
n
t
 
t
o
a
n
y
 
o
b
j
e
c
t
 
t
h
a
t
 
w
a
s
i
n
h
e
r
i
t
e
d
 
f
r
o
m
 
t
h
a
t
 
c
l
a
s
s
.
F
o
r
 
e
x
a
m
p
l
e
Slide Note
Embed
Share

Learn about creating and working with arrays, defining functions, and building classes in an object-oriented programming context. Dive into examples like creating arrays of objects, defining arrays with pointers, and designing classes for a publishing company that markets books and audio cassettes. Follow along to trace the provided C++ program step by step and explore extra examples for further understanding.

  • Object-oriented programming
  • Arrays
  • Functions
  • Classes
  • C++

Uploaded on Aug 27, 2024 | 5 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Array of objects

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

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

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

  5. 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.

  6. 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.

  7. Extra Examples Self read

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

  9. 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(); }

  10. Trace the program. void { 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; chorus(*ptr,ptr,*ptr); cout << endl << endl; delete ptr; ////////////////////////////////////////// ptr = new Cat; cout << "Cat Created" << endl; cout << "Cats singing" << endl; chorus(*ptr,ptr,*ptr); cout << endl << endl; main() delete ptr; }

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

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

  13. void { Pet *ptr; //Pointer to base class main() /* 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; chorus(ptr,*ptr); cout << endl << endl; delete ptr; ////////////////////////////////////////// ptr = new Cat; cout << "Cat Created" << endl; cout << "Cats singing" << endl; chorus(ptr,*ptr); cout << endl << endl; delete ptr; }

  14. 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

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#