9. classes and objects

9. classes and objects - ENGG1002 Computer Programming and...

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

View Full Document Right Arrow Icon
ENGG1002 Computer Programming and Applications Prepared by Dr. K.K.Y. Wong
Background image of page 1

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

View Full DocumentRight Arrow Icon
Classes and Objects A class is an expanded concept of a structure It consists of both member variables and member functions that operate on its member variables An object is an instance of a class In terms of data types and variables, a class would be the type , and an object would be the content (data) of a variable Classes and objects are the basic building blocks in object- oriented programming ( OOP ) which uses objects and their interactions to design a computer program 2
Background image of page 2
Classes and Objects Objects instantiated (created) from the same class Can perform the same actions (same member functions ) Have the same attributes (same member variables ) Similar to variables, the data stored in the member variables of an object may change over time (due to the actions of its member functions) Different objects instantiated from the same class can contain different values in their member variables 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Definitions In C++, a class is defined using the keyword class and the syntax is similar to that for defining a structure Like in structure definitions, the object names before the ending semicolon are used for object declarations and are optional 4 Syntax class class_name { access_specifier_1: access_specifier_2: } obj_name_1, obj_name_2, …;
Background image of page 4
Definitions Example: 5 class CPoint { public: double x; double y; void setValue(double x_, double y_) { x = x_; y = y_; } } pt1, pt2; class name: CPoint access specifier member variables: double x , double y Instances (objects) of the CPoint class: pt1 , pt2 member function: void set_val(double, double)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Public vs Private Access specifiers modify the access rights of the members listed after them The keyword public indicates that the members of the class are publicly accessible from anywhere within the scope of the object The keyword private indicates that the members of the class are only accessible from within other members of the same class By default, all members of a class are private The use of private member variables avoids illegal access or change of the variables from outside the class A good practice is to make all member variables private , and access or change them using public member functions 6
Background image of page 6
Public vs Private Example: 7 #include <iostream> using namespace std; class CPoint { public: void setValue(double x_, double y_) { x = x_; y = y_; } private: double x, y; }; int main() { CPoint pt; pt.x = 1.0; pt.y = 2.0; return 0; } The compiler will give an error message as x and y are private members of pt
Background image of page 7

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

View Full DocumentRight Arrow Icon
Scope Operator A member function of a class can also be defined outside the class definition by using the scope operator :: Only the function declaration is needed to be included in the class definition 8 Syntax type_ret class_name::func_name(type_1 par_1, type2 par_2, …) { statement_1; statement_2; }
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/26/2011 for the course BENG 1002 taught by Professor Dr.x during the Winter '10 term at HKU.

Page1 / 29

9. classes and objects - ENGG1002 Computer Programming and...

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

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