Академический Документы
Профессиональный Документы
Культура Документы
Their size in memory. What information they can hold. What actions can be performed on them
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(); };
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)
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
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