Lecture04-Class_Part_1 - Introduction to Object-Oriented...

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

View Full Document Right Arrow Icon
Introduction to Object-Oriented Programming Classes 1 Computer Programming II 1 Lecture 4
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Learning Objectives n To understand abstraction in C++ n To understand and declare a class in C++ n To understand and implement e ncapsulation in C++ n To declare and implement C++ method n How to comment a class n To understand how to p lace method definition outside the class n To understand scope and methods n To understand when to use this Pointer
Background image of page 2
3 3 4 Principles of OOP 1. Abstraction – focus only on information important to the problem 2. Encapsulation – combine data and operations in a single unit, and hide the implementation from user 3. Inheritance – create new classes from existing 4. Polymorphism – the ability to use the same expression to denote different operations. (Lecture 10 & 11)
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Abstraction in C++ n Abstraction is the process of separating the logical properties from the implementation details . n Logical properties are the information important to the problem n E.g. driving is a logical property of a car; the construction of the engine constitutes the implementation details . n We have an abstract view of what the engine does, but are not interested in the engine’s actual details implementation. n The notions of abstraction simplifies modeling and working on the problem.
Background image of page 4
5 Declaring a class in C++ n Recall that a class of objects is a group of objects that have a set of: 1. attributes / data member/variables/states, and 2. behaviors/methods/member functions/operations . n In C++, object classes are created using the class data type.
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Declaring a class in C++ n The class diagram for the Car class is as below Car -model:string -color:string -speed:int +drive ( ) +stop ( ) +turn ( ) l The first step is to define a new class for our car: class Car { }; Class name attributes methods
Background image of page 6
7 Declaring a class in C++ n Next, add the attributes (data member) which we have defined for a car. n For the car object class, we define that model and color are of type string , and speed is an int. class Car { string model; string color; int speed; }; Car -model:string -color:string -speed:int +drive ( ) +stop ( ) +turn( )
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Encapsulation n Encapsulation is the idea that the internal workings of an object can be hidden from the outside world. n Encapsulation is performed in two ways: 1. We encapsulate the details of how attributes are stored. 2. We encapsulate the details of how methods are performed. n Why would we want to encapsulate objects? n To reduce the need for outside users to worry about how something is done. n This provides significant benefits in program maintenance – we can change those details without the users ever being aware (provided that the outside interface remains the same).
Background image of page 8
9 Encapsulation in C++ n Encapsulation is embodied in C++ class ’s by restricting access to an object’s members. n
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/03/2012 for the course IT 1241 taught by Professor Pang during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 35

Lecture04-Class_Part_1 - Introduction to Object-Oriented...

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

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