cs31s11dis9 - CS31 Introduction to Computer Science I...

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

View Full Document Right Arrow Icon
Discussion 1H Notes (Week 9, May 27) TA: Brian Choi ([email protected]) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Classes We have seen different types of variables so far – int , double , string , etc. But these basic types are not enough to represent everything. We might want to throw in things as random as cats, dogs, bears, apples, and trees into my program (quite literally). You will soon see these animals and plants in your program. We’ll call these customized types classes . We will start with a class with minimal functionalities, and keep adding more components onto it to make it more complete. Basics A class is a construct used to group related Felds (variables) and methods (functions). More technically, it is a collection of variables (which may be of different types) and possibly some functions associated with them, put together to serve some speciFc purposes. This may not sound intuitive in words. Let us create a cat for an example. class Cat { public: int m_age; void meow(); }; Ignore the line public: for now. Within this Cat class, there are two members – a member variable m_age and a member function named meow() . One can access these members by using a dot, as done with structures. See the example below: CS31: Introduction to Computer Science I Spring 2011 Copyright Brian Choi 2011. All Rights Reserved. Week 9, Page 1/8 class Cat { public: int m_age; // Stores the age. void meow(); // Prints “MEOW!” and increments the age by 1. }; // Don’t forget the semicolon! int main() { Cat kitty1; // A Cat instance. Cat kitty2; // Another Cat instance. kitty1.m_age = 1; // This cat is 1 year old. kitty2.m_age = 3; // This cat is 3 years old. cout << "Kitty1 is " << kitty1.m_age << " years old." << endl; cout << "Kitty2 is " << kitty2.m_age << " years old." << endl; kitty1.meow(); cout << "Kitty1 is " << kitty1.m_age << " years old." << endl; kitty1.meow(); cout << "Kitty1 is " << kitty1.m_age << " years old." << endl; }
Background image of page 1

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

View Full DocumentRight Arrow Icon
Output: Hopefully the above example is intuitive. A few things to note: • Note that every instance of a class (in the example, kitty1 and kitty2 ) has its own copy of members. • To add to the above bullet, m_age and meow() by themselves do not make much sense in main() . You must indicate which instance these members belong to. (e.g., kitty1.m_age ) • By convention, we capitalize the Frst letter of class name. (e.g., Cat , Dog , OakTree ) Wait, isn’t something missing? The above class deFnition is incomplete without the deFnition of the function meow() . Let us deFne it now. Member Functions Here is the definition of meow() (to be filled in in class). void Cat::meow() { } Notice the new syntax for function header. This is really no different from defining other functions, but the name of the class is included to indicate the membership of the function. The general syntax for it is: return_type Class_name:: function_name( argument_list ) { } Within the definition of a member function , you can access and manipulate all
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 8

cs31s11dis9 - CS31 Introduction to Computer Science I...

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

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