<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 17 Classes, Members and Methods_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 17: Classes, Members and Methods
 More of this Feature
• Introduction
• Defining Classes
• Defining Classes, Continued
• Overloaded Methods and Default Arguments
• Overloaded Methods and Default Arguments, Cont.
• Classes as Members
• Classes as Members, Cont.
 
 Related Resources
• C++ Tutorial - All lessons.
• C++ Tutorial - Introduction to Classes
• C++ Tutorial - Function Overloading
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
 

by John Kopp

Welcome to About.com's free tutorial on C++ programming. This lesson is the first of several that will teach all you need to know to define and use classes and objects in C++. Classes and objects have been seen in earlier lessons and were introduced in lesson 9, Introduction to Classes. You may wish to review that lesson before proceeding.

The use of objects in C++ defines the way programs are designed and written. Classes are a software construct that can be used to emulate a real world object. Any real world object can be defined by its attributes and by its actions. For instance, a cat has attributes such as its age, weight, and color. It also has a set of actions that it can perform such as sleep, eat and complain (meow). The class mechanism in C++ provides a way to create "cats" that we can use in our program. Obviously, no software construct can perfectly emulate a real object, but a goal in designing classes is to have all the relevant attributes and abilities encapsulated by the class. This way, objects of a class are easily created and used. In essence, a ready-made cat is also available to fulfill any requirements.

The difference between a class and an object can be confusing to beginners. A class is a programmer defined data type that encapsulates data and abilities. For instance, a software model of a car, a car "class", might contain data about the type of car and abilities such as accelerate or decelerate. An object is a particular instance of a class. This is best understood by an analogy with a built in data type such as int.

int x;

Declares x to be a variable of type int.

Car impala;

Declares impala to be an object of class Car.

Only an object represents an actually entity that can be manipulated, initialized or assigned. The class is a data type.

A number cannot be assigned to "int", since it is a data type and not a variable.

int = 52;    // Incorrect

int x;    // Correct, declares a variable, x, of built-in type int.
x = 52;


Similarly, the "class" generally should not appear in expressions. The exception to this will be seen later in a later lesson covering static members and methods, which are class wide.

Car = impala;    // Incorrect

Car myCar;    //Correct, defines a object, myCar, of user defined class Car.
myCar = impala;    //Assigns impala to myCar.



Next page > Defining Classes > Page 1, 2, 3, 4, 5, 6, 7


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.