cs31 lecture 11

Methodnamearguments x calling a public method of a

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: class (from outside): <objectname>.<methodname>([arguments]) x Calling a public method of a class (from inside): 9 A first example struct Car { int uniqueId; int void displayId(); void }; }; void Car::displayId() { void std::cout << uniqueId << std::endl; std::cout } int main() { Car c; Car c.uniqueId = 123; c.uniqueId c.displayId(); c.displayId(); } 10 Classes and namespaces x They are not to be confused!! x A namespace is just a prefix around the names of types / variables x A class is a type, and objects/variables of this type can be declared x Namespaces and classes can be combined: namespace foo { int global1; int struct bar { struct int attrib1; int }; }; } foo::bar object1; foo::bar object2; object1.attrib1 = 42; object2.attrib1 = 43; foo::global1 = 1; 11 The lifetime of an object x Example for a variable: void foo() { int bar; // The variable is declared. A data region is allocated to store /// it. / bar += 42; // We use the variable (reading/writing its content) /// The function terminates. The data region for ‘bar’ is freed / } x Example for an object: void foo() { myInt bar; // The variable is declared. A data region is allocated to store /// it. Then a specific method is called: the constructor / bar += 42; // We use the variable (readin...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online