Sams Teach Yourself C++ in 10 Minutes

( 2 )


C++ remains a popular object-oriented language and Sams Teach Yourself C++ in 10 Minutes provides a quick, focused way to learn the language. The author assumes no prior programming experience and begins by showing the reader the absolute fundamentals of what a program is and guides the readers to write their very first program. It covers all the important topics in C++ and provides a sold foundation on which to build programming knowledge. The material is reorganized to fit proven teaching techniques with about ...

See more details below
Paperback (Subsequent)
$24.21 price
(Save 19%)$29.99 List Price

Pick Up In Store

Reserve and pick up in 60 minutes at your local store

Other sellers (Paperback)
  • All (34) from $1.99   
  • New (6) from $15.85   
  • Used (28) from $1.99   
Sending request ...


C++ remains a popular object-oriented language and Sams Teach Yourself C++ in 10 Minutes provides a quick, focused way to learn the language. The author assumes no prior programming experience and begins by showing the reader the absolute fundamentals of what a program is and guides the readers to write their very first program. It covers all the important topics in C++ and provides a sold foundation on which to build programming knowledge. The material is reorganized to fit proven teaching techniques with about thirty percent of the text rewritten. All examples are new to comply with the current ANSI C++ standard and make them completely compatible with all popular compilers.

Read More Show Less

Product Details

  • ISBN-13: 9780672324253
  • Publisher: Sams
  • Publication date: 6/28/2002
  • Series: Sams Teach Yourself Series
  • Edition description: Subsequent
  • Edition number: 2
  • Pages: 320
  • Sales rank: 960,407
  • Product dimensions: 5.50 (w) x 8.24 (h) x 0.70 (d)

Meet the Author

Jesse Liberty is president of Liberty Associates, Inc., which provides onsite training, mentoring, consulting, and contract programming. Jesse was a software architect for Xerox and a distinguished software engineer for AT&T. He is the author of numerous books on C++ and object-oriented programming, the latest of which are:

  • Sams Teach Yourself C++ in 21 Days, 4E, 067232072X, March 2001
  • Sams Teach Yourself C++ in 24 Hours, 3E, Starter Kit, 0672322242, August 2001

Mark Cashman is a software developer with experience in a variety of industries. His specialties include computer/human interaction, elational databases, object-oriented development, and Web site/Web software development, including his own site, the Temporal Doorway. Mark is an adjunct professor at Springfield College and a co-dean of in-house training at Ultimate Data Systems. He speaks and teaches at seminars on C++. He is a contributing author to:

  • C++ Builder 5 Developer's Guide, 0672319721, Sams Publishing, November 2000
Read More Show Less

Table of Contents

Introduction 1
1 Getting Started 5
2 Output to the Console - Standard Output 14
3 Calculations 20
4 Numeric Input 30
5 if Statements and Program Decisions 36
6 Exception Handling 42
7 Functions 46
8 Separating Code into Modules 57
9 do/while Loops 66
10 Nested Loops and Complex boo1 Expressions 73
11 switch Statements, static Variables, and runtimeƶerrors 81
12 Arrays, Loops, and the Increment and Decrement Operators 88
13 Storage: Heaps, Stacks, and Pointing 95
14 Testing 108
15 Structures and Types 121
16 File I/O 136
17 Classes: Structures with Functions 145
18 Refactoring the Calculator with Classes 157
19 Implementing the Calculator as Classes 163
20 The Rest of the Calculator as Classes 179
21 Function and Operator Overloading 192
22 Inheritance 206
23 Object Testing Using Inheritance 223
24 Abstract Classes, Multiple Inheritance, and Static Members 229
25 Templates 247
26 Performance: C++ Optimizations 263
27 Wrapping Up 270
A Operators 275
B Operator Precedence 286
Index 289
Read More Show Less

First Chapter

[Figures are not included in this sample chapter]

Sams Teach Yourself C++ in 10 Minutes
- 7 -

In this lesson, you will learn what classes and objects are and how to definea new class and create objects of that class. Classes extend the built-in capabilitiesof C++ to assist you in representing and solving complex, real-world problems.


The type of a variable tells you its size and also the capabilities of the object.For example, integers can be added together. Thus, just by declaring Height and Widthto be integers, you know that it is possible to add Height to Width and to assignthat number to another number.

A type is a category. Familiar types include car, house, person, and shape.In C++, a type is an object with a size, a state, and a set of abilities.

A C++ programmer can create any type needed, and each of these new types can haveall the functionality and power of the built-in types.


Programs are usually written to solve real-world problems, such as keeping trackof employee records or simulating the workings of a heating system. Although it ispossible to solve these problems by using programs written with only integers andcharacters, it is far easier if you can create represent ations of the objects thatyou are talking about. In other words, simulating the workings of a heating systemis easier if you can create variables that represent rooms, heat sensors, thermostats,and boilers. The closer these variables correspond to reality, the easier it is towrite the program.


You make a new type by declaring a class. A class is just a collectionof variables--often of different types--combined with a set of related functions.

Clients of your class are other classes or functions that make use of your class.Encapsulation allows the clients of your class to use it without knowing orcaring about how it works. They only need to know what it does, not how itdoes it.

Encapsulation  The bundling together of all the information, capabilities, and responsibilities of an entity into a single object.

A class can consist of any combination of the variable types and also other classtypes. The variables in the class are referred to as the member variablesor data members.

The functions in the class typically manipulate the member variables. They arereferred to as member functions or methods of the class.


To declare a class, you use the class keyword followed by an opening brace andthen list the data members and methods of that class. End the declaration with aclosing brace and a semicolon. Here's the declaration of a class called Cat:

class Cat{public:    unsigned int  itsAge;    unsigned int  itsWeight;    Meow();};

Declaring this class doesn't allocate memory for a Cat. It just tells the compilerwhat a Cat is, what data it contains (itsAge and itsWeight), and what it can do (Meow()).It also tells the compiler how big a Cat is--that is, how much room the compilermust set aside for each Cat that you create. In this example, if an integer is 4bytes, a Cat is only 8 bytes big: itsAge is 4 bytes, and itsWeight is another 4.Meow() takes up no room because no storage space is set aside for member functions(methods).


You define an object of your new type just as you define an integer variable:

unsigned int GrossWeight;       // define an unsigned integerCat Frisky;                       // define a Cat

An object is simply an individual instance of a class.


Once you define an actual Cat object (for example, Frisky), you use the dot operator(.) to access the members of that object. Therefore, to assign 50 to Frisky's itsWeightmember variable, you would write

Frisky.itsWeight = 50;

In the same way, to call the Meow() function, you would write



Other keywords are used in the declaration of a class. Two of the most importantare public and private.

All members of a class--data and methods--are private by default. Private memberscan be accessed only within methods of the class itself. Public members can be accessedthrough any object of the class. This distinction is both important and confusing.To make it a bit clearer, consider an example from earlier in this chapter:

class Cat{    unsigned int  itsAge;    unsigned int  itsWeight;    Meow();};

In this declarati on, itsAge, itsWeight, and Meow() are all private, because allmembers of a class are private by default. This means that unless you specify otherwise,they are private.

However, if you write

Cat  Boots;Boots.itsAge=5;    // error! can't access private data!

the compiler flags this as an error. In effect, you've said to the compiler, "I'llaccess itsAge, itsWeight, and Meow() from only within member functions of the Catclass." Yet here you've accessed it from outside a Cat method. Just becauseBoots is an object of class Cat, that doesn't mean that you can access the partsof Boots that are private.

The way to use Cat so that you can access the data members is to declare a sectionof the Cat declaration to be public:

class Cat{public:    unsigned int  itsAge;    unsigned int  itsWeight;    Meow();};

Now itsAge, itsWeight, and Meow() are all public. Boots.itsAge=5 compiles withouta problem.


As a general rule of design, you should keep the member data of a class privateand write public methods to set and get their value. These methodsare often called accessor methods.


Every class method that you declare must also be defined.

A member function definition begins with the name of the class followedby two colons, the name of the function, and its parameters. Listing 7.1 shows thecomplete declaration of a simple Cat class and the implementation of its accessorfunction and one general class member function.


1:   // Demonstrates declaration of a cla ss and2:   // definition of class methods,3:4:   #include <iostream.h>      // for cout5:6:   class Cat            // begin declaration of the class7:   {8:   public:                   // begin public section9:       int GetAge();           // accessor function10:      void SetAge (int age);  // accessor function11:      void Meow();            // general function12:  private:                  // begin private section13:      int itsAge;             // member variable14:  };15:16:  // GetAge, Public accessor function17:  // returns value of itsAge member18:  int Cat::GetAge()19:  {20:     return itsAge;21:  }22:23:  // definition of SetAge, public24:  // accessor function25:  // returns sets itsAge member26:  void Cat::SetAge(int age)27:  {28:     // set member variable its age to29:     // value passed in by parameter age30:     itsAge = age;31:  }32:33:  // definition of Meow method34:  // returns: void35:  // parameters: None36:  // action: Prints "meow" to screen37:  void Cat::Meow()38:  {39:     cout << "Meow.\n";40:  }41:42:  // create a cat, set its age, have it43:  // meow, tell us its age, then meow again.44:  int main()45:  {46:     Cat Frisky;47:     Frisky.SetAge(5);48:     Frisky.Meow();49:     cout << "Frisky is a cat who is " ;50:     cout << Frisky.GetAge() << " years old.\n";51:     Frisky.Meow();52;     return 0;53:  }Meow.Frisky is a cat who is 5 years old.Meow.

Lines 6-14 contain the definition of the Cat class. Line 8 contains the keywordpublic, which tells the compiler that what follows is a set of public members. Line9 has the declaration of the public accessor method GetAge(). GetAge() provides acce ssto the private member variable itsAge, which is declared in line 13. Line 10 hasthe public accessor function SetAge(). SetAge() takes an integer as an argument andsets itsAge to the value of that argument.

Line 12 begins the private section, which includes only the declaration in line13 of the private member variable itsAge. The class declaration ends with a closingbrace and semicolon in line 14.

Lines 18-21 contain the definition of the member function GetAge(). This methodtakes no parameters; it returns an integer. Note that class methods include the classname followed by two colons and the function's name (line 18). This syntax tellsthe compiler that the GetAge() function you are defining here is the one that youdeclared in the Cat class. With the exception of this header line, the GetAge() functionis created like any other function.

The GetAge() function takes only one line; it returns the value in itsAge. Notethat the main() function cannot access itsAge because itsAge is private to the Catclass. The main() function has access to the public method GetAge(). Because GetAge()is a member function of the Cat class, it has full access to the itsAge variable.This access enables GetAge() to return the value of itsAge to main().

Lines 26-31 contain the definition of the SetAge() member function. It takes aninteger parameter and sets the value of itsAge to the value of that parameter inline 30. Because it is a member of the Cat class, SetAge() has direct access to themember variable itsAge.

Line 37 begins the definition, or implementation, of the Meow() method of theCat class. It is a one-line function that prints the word Meow to the screen, followedby a new line. (Remem ber that the \n character prints a new line to the screen.)

Line 44 begins the body of the program with the familiar main() function. In thiscase, it takes no arguments and returns int. In line 46, main() declares a Cat namedFrisky. In line 47, the value 5 is assigned to the itsAge member variable by wayof the SetAge() accessor method. Note that the method is called by using the classname (Frisky) followed by the member operator (.) and the method name (SetAge()).In this same way, you can call any of the other methods in a class.

Line 48 calls the Meow() member function, and lines 49 and 50 print a messageusing the GetAge() accessor. Line 51 calls Meow() again.

In this lesson, you learned what classes and objects are and how to define a newclass and create objects of that class.

Read More Show Less

Customer Reviews

Average Rating 4.5
( 2 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 2 Customer Reviews
  • Anonymous

    Posted March 5, 2001

    Best Yet!

    In response to the previous review: Read the intro: each module is designed to take you approx. ten minutes to go through. I am only half way through reading on 2 lunch breaks and I find this to be accurate. This is the most clear and concise explaination of Objects and Classes I have read anywhere! It is for the extreme novice, but some knowledge of computers is helpful. There are some confusing aspects in the presentation, but they are few. if you want to understand C++ and get a clear definition of programese - buy this book.

    1 out of 1 people found this review helpful.

    Was this review helpful? Yes  No   Report this review
  • Anonymous

    Posted January 2, 2010

    No text was provided for this review.

Sort by: Showing all of 2 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)