Session_8 - Lesson 10 Classes and Objects When you declare a class you are creating a new type A class is just a collection of variables(often of

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lesson 10 – Classes and Objects When you declare a class you are creating a new type. A class is just a collection of variables (often of different types) together with a set of related functions. Structures We’ll go out of sequence from the book and introduce Structures. You know about int, long, char but you can also have arrays of integers. Here’s an example: #include <iostream> using namespace std; int main() { int i; int a[50]; i = 10; a[0] = 1; a[1] = 2; a[2] = 49; cout << a[0] << '\n' << a[1] << '\n' << a[2] << endl; return 0; } The array a[50] holds only integers. What if you want to collect together variables of different types. Although an array can hold several items, each item has to be of the same type. That is, one array can hold twenty ints and another can hold ten floats, but a single array can’t store ints in some elements and floats in other elements. The answer is a C++ structure . The structure is more versatile than an array because it can hold several kinds of data. The structure is a good stepping-stone to Classes and is presented out of sequence in these notes. Once you define a new type, you can create variables of that type. Thus creating a structure is a two-part process. First, you define a structure description then you create a structure variable (called object with classes) that follows the template of the structure. Page 1 of 18
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
For example: struct product { char code; float volume; double price; }; Here’s its usage: #include <iostream> using namespace std; struct product { char code; float volume; double price; }; int main() { product x; x.code = 'a'; x.volume = 25.5; x.price =6500; cout << "Product code is " << x.code << endl; cout << "Product volume is " << x.volume << endl; return 0; } You create variables of type product just as you create int or char. C++ allows you to drop the keyword struct, that is struct product x; The keyword struct is not necessary, although accepted. The introduction of product has extended our type names by adding a new type. When you use struct members, you use the membership operator (.) to access individual members. For example, x.code Page 2 of 18
Background image of page 2
Return to Classes You declare a Class much as you did a Structure. A class need not contain functions and could look like a structure, or it could contain functions as well as in: class Cat { unsigned int itsAge; unsigned int itsWeight; void Meow() { }; }; Declaring this class doesn’t allocate memory for a Cat, it just tells the compiler to extend its available types and what a Cat Class is. You then declare a Cat object with Cat Frisky; // define a Cat In a similar way that you would declare an int: int FirstName; // define an int The following program won’t compile (will explain later): #include <iostream> using namespace std; class Cat { unsigned int itsAge; unsigned int itsWeight; void Meow() { }; }; int main() { Cat Frisky; Frisky.itsAge = 5; cout << "Frisky is a cat who is " ; cout << Frisky.itsAge << " years old.\n" ; return
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/07/2010 for the course CS 175 taught by Professor Mike during the Spring '06 term at Golden West College.

Page1 / 18

Session_8 - Lesson 10 Classes and Objects When you declare a class you are creating a new type A class is just a collection of variables(often of

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online