{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Session_8 - Lesson 10 Classes and Objects When you declare...

Info icon This 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
Image of page 1

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

View Full Document Right 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
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 0; } Remember you assign to objects, not classes. That is, Frisky.itsAge = 10; not Cat.itsAge = 10; // wrong!
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern