<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 21 Arrays of Class Objects_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 21: Arrays of Class Objects
 More of this Feature
• Arrays of Class Objects - Intro, Using Single Objects
• Arrays of Class Objects - Using Single Objects, Arrays
• Arrays of Class Objects - Arrays, continued
 
 Related Resources
• C++ Tutorial - Introduction to Classes
• C++ Tutorial - Arrays and Vectors
• C++ Tutorial - Dynamic Memory Allocation
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial - See all lessons
 

by John Kopp

Introduction
Welcome to About.com's free tutorial on C++ programming. In previous lessons we have seen how to create arrays and single objects of automatic extent and how to dynamically allocated arrays and single objects for objects of built-in types. An automatic object has its storage allocated when it is defined. These are just the ordinary objects we have been using through out this tutorial. This lesson extends these concepts to include creating and using arrays of class objects. Let's begin with a quick review of defining and using single class objects and then work with arrays.

Review: Defining and Using Class Objects
The simplest way to learn how to define and use classes is to study a simple example. Let's define a simple Cat class, create a few felines and see what they can do. But, first, let's quickly look at dynamically allocating class objects. We saw the use of new and delete with built-in types in a previous lesson. The operator new dynamically creates an object on the heap and returns a pointer to it. The operator delete is used to free that memory after it is no longer needed so that it may be reused elsewhere by the program. The following syntax is used to dynamically allocate a class object.

class pt = new class;    //Calls default constructor
class pt = new class(arg1, arg2, ....);    /* Calls constructor with a parameter list that matches the number and type of the arguments in the call. */

To delete the dynamically allocated object, that is, to free the memory it occupies:

delete pt;
    //Where pointer contains the address of the object to be freed.

Hiss!!. Here is a brief example.

#include <iostream>
#include <string>
using namespace std;

class Cat {
public:
    Cat(string name = "tom", string color = "black_and_white") : _name(name), _color(color) {}
    ~Cat() {}
    void setName(string name) {_name = name;}
    string getName() {return _name;}
    void setColor(string color) {_color = color;}
    string getColor() {return _color;}
    void speak() {cout << "meow" << endl;}
private:
    string _name;
    string _color;
};

int main()
{
    Cat cat1("morris","orange");    //Objects of automatic extent exist on stack.
    Cat *cat2pt = new Cat("felix","black");    /* Dynamically allocated objects exist on the heap. */
    Cat *cat3pt = new Cat;    //Calls default constructor

    cout << cat1.getName() << " is " << cat1.getColor() << endl;
    cout << cat2pt->getName() << " is " << cat2pt->getColor() << endl;
    cout << cat3pt->getName() << " is " << cat3pt->getColor() << endl;

    cat1.speak();
    cat2pt->speak();

    delete cat2pt;    //Always "delete" dynamically allocated objects!
    delete cat3pt;

    return 0;
}



Next page > Arrays of Class Objects - Using Single Objects, Arrays > Page 1, 2, 3


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.