cs31 lecture 11

X a class is a type x an object is an instance 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: ct? x A class is a type x An object is an instance of a class x It has an internal state (attributes) x It has functions associated to it (methods) x There can be multiple objects of the same class in the There program program std::string foo; // a first object of type string std::string bar; // a second object of type string 6 What is a class? x A class is a type, and you can declare variable (objects) of class this type this x A class in OOP can have: Attributes (fields), to specify data to be attached to the class Methods (functions), that can operate on the data in the class, and Methods on external data on x A class in C++ can be declared with The keyword struct (all methods are public by default) The keyword class (all methods are private by default) 7 More on the syntax x Declaring a class with all attributes/methods public: struct <classname> { [attributes declaration, methods prototype] [attributes }; x Implementation of a method: <type> <classname>::<methodname>([arguments]) { … } 8 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...
View Full Document

Ask a homework question - tutors are online