cs31 lecture 17

A data region is allocated to store it bar 42

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: e] [attributes }; x Implementation of a method: <type> <classname>::<methodname>([arguments])
 { … } 115 More on the syntax x Declaring an instance of a class in a program <type> <objectname>; x Getting/setting the value of a public attribute of a class from outside Getting/setting its implementation its <objectname>.<attributename> x Getting/setting the value of am attribute of a class from inside its Getting/setting implementation implementation <attributename> x Calling a public method of a class (from outside)
: <objectname>.<methodname>([arguments])
 x Calling a public method of a class (from inside)
: 116 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()
; } 117 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; 118 The lifetime of an object x Example for a variable: void foo()
 { int bar; // The variable is declared. A data region is...
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