Вы находитесь на странице: 1из 6

Types of Variables: What do They Imply

Their size in memory. What information they can hold. What actions can be performed on them

Why Create a New Type?


It is easier to solve large, complex problems if you can create variables or representations of the objects that you deal with The closer these variables correspond to reality, the easier it is to write the program.

Classes and Members


A class is a collection of
Variables, often of different types, combined with a set of related functions

A class enables you to encapsulate, or bundle, various types of variables and various functions into one collection, which is called an object Encapsulating allows you to put all information you know about an object into one class making it easy to refer to, copy, and manipulate the data

Components of a Class
A class can consist of any combination
variable types and other class types

The variables in the class are referred to as the member variables or data members The functions in the class typically manipulate the member variables. They are referred to as member functions or methods of the class
Member functions are as much a part of your class as the member variables. They determine what the objects of your class can do Clients of your class--that is, the parts of the program that use your class--can use your object without worry about what is in it or how it works

Declaring a Class
To declare a class, use the class keyword followed by an opening brace, and then list the data members and methods of that class End the declaration with a closing brace and a semicolon. Here's the declaration of a class called Cat: class Cat { unsigned int itsAge; unsigned int itsWeight; Meow(); };

Memory Allocation for Class Declaration


Declaring the Cat class doesn't allocate memory for a Cat It tells the compiler what a Cat is i.e.
what data the Cat class contains (itsAge and itsWeight) And what the cat class can do (Meow()).

It also tells the compiler how big a Cat is:


that is, how much room the compiler must set aside for each Cat that you create

In this example, if an integer is two bytes, a Cat is only four bytes big:
itsAge is two bytes and itsWeight is another two bytes. Meow() takes up no room, because no storage space is set aside for member functions (methods)

A Word on Naming Conventions


naming member variables, member functions, and classes: use easily understood and meaningful names Cat, Rectangle, and Employee are good class names Meow(), ChaseMice(), and StopEngine() are good function names, because they tell you what the functions do. Many programmers name the member variables with the prefix its, as in itsAge, itsWeight, and itsSpeed. This helps to distinguish member variables from nonmember variables.

Defining an Object
You define an object of your new type just as you define an integer variable: unsigned int GrossWeight; unsigned integer Cat Frisky; This code defines a variable called Gross Weight whose type is an unsigned integer. It also defines Frisky, which is an object whose class (or type) is Cat. Frisky is an object of type Cat in the same way in which GrossWeight is a variable of type unsigned int

Accessing Class Members


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 Weight member variable, you would write Frisky.Weight = 50; In the same way, to call the Meow() function, you would write Frisky.Meow(); When you use a class method, you call the method. In this example, you are calling Meow() on Frisky.

int = 5; // wrong In C++ you don't assign values to types; you assign values to variables int x; // define x to be an int x = 5; // set x's value to 5 you must define an integer variable and assign 5 to that variable Cat.age=5; // wrong you can't assign 5 to the age part of a Cat Cat Frisky; // just like int x; Frisky.age = 5; // just like x = 5; you must define a Cat object and assign 5 to that object

Assign to Objects, Not to Classes

Вам также может понравиться