<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 20 Copy Constructors_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 20: Copy Constructors
 More of this Feature
• Copy Constructors
• Copy Constructors, continued
 
 Related Resources
• C Tutorial - Strings
• C++ Tutorial - Constructors and Destructors
• 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. This lesson is covers copy constructors. In previous lessons we saw how to write and use constructors for objects. A copy constructor is a special constructor that takes as its argument a reference to an object of the same class and creates a new object that is a copy. By default, the compiler provides a copy constructor that performs a member-by-member copy from the original object to the one being created. This is called a member wise or shallow copy. Although it may seem to be the desired behavior, in many cases a shallow copy is not satisfactory. To see why let's look at the Employee class introduced in an earlier lesson with one change. We will store the name in a C-style character string rather than store the employee name using the string class from the standard C++ library. Here is a simple program with a bare bones version of the Employee class.

#include <iostream>
using namespace std;

class Employee {
public:
    Employee(char *name, int id);
    ~Employee();
    char *getName(){return _name;}
    //Other Accessor methods
private:
    int _id;
    char *_name;
};

Employee::Employee(char *name, int id)
{
    _id = id;

    _name = new char[strlen(name) + 1];
            //Allocates an character array object
    strcpy(_name, name);
}

Employee::~Employee()
{
    delete[] _name;
}

int main()
{
    Employee programmer("John",22);
    cout << programmer.getName() << endl;
    return 0;
}


The function strlen returns the length of the string passed into the constructor. Notice that the Employee name is now stored in a dynamically allocated character array. It is of "string length + 1" to allow for the null terminator used with C-style strings, '\0'. The strcpy function automatically adds the null terminator to the destination string. Also, notice that the destructor frees the memory used to hold the employee name. This is needed to avoid a memory leak, which was described in the last lesson. Please see C Tutorial - Lesson 10: Strings for more detail on C-style strings. They are used in C++, particularly for command-line arguments. This will be covered in a later lesson.



Next page > Copy Constructors, Continued > Page 1, 2


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.