WritingaGood Program5.ObjectsandClasses inC++
Writing a Good Program 5. Objects and Classes in C++
ComputerProgrammingandBasicSoftwareEngineering5.Objects andClasses inC++5.1 BasicObjectOrientedProgramming
5.1 Basic Object Oriented Programming Computer Programming and Basic Software Engineering 5. Objects and Classes in C++
ComputerProgrammingandBasicSoftwareEngineering5.Objects and Classes in C++WhatisanObject?The real world is composed of different kinds ofobjects: buildings, men, women, dogs, cars, etc.Each object has its own states and behaviors.BehaviorsCOColor=RedBrand=FerrariChangingSpeed=200mphSteeringGearGear= 4StatesBrakingAcceleratingVariablesandfunctions
What is an Object? • The real world is composed of different kinds of objects: buildings, men, women, dogs, cars, etc. • Each object has its own states and behaviors. Color = Red Brand = Ferrari Speed = 200 mph Gear = 4 States Accelerating Braking Changing Gear Steering Behaviors Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Variables and functions
ComputerProgrammingandBasicSoftwareEngineering5.ObjectsandClassesinC++WhatisaSoftwareObject?Software designers use the same idea to easeprogrammers to develop their softwareSoftware is also composed of different kind ofsoftware objectsEach software object also has its own states andbehaviors.ButtonMethodVariables (States)Color = Grey(Behavior)size = 2cm x 2cmPress( )Shape = Rectangular(protruded)
What is a Software Object? • Software designers use the same idea to ease programmers to develop their software • Software is also composed of different kind of software objects • Each software object also has its own states and behaviors. Variables (States) Color = Grey Size = 2cm x 2cm Shape = Rectangular Method (Behavior) (protruded) Press( ) Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Button
ComputerProgrammingandBasicSoftwareEngineering5.Objects and Classes in C++EncapsulationHiding information within an object's nucleusProvide a public interface for interacting with itAdvantages to software developersModularity:An object can be easily passed around in thesystem (Otherwise, you need to think about howmanyfilesyou need to bundletogethertopass toLibraryReusabilityyourfriends)Information hiding: Users need not go into details of theobject before using the object (E.g., you don't need toknow the circuit of a TV setif you wantto watch TVSafety: Users of the object may not directly access theinternal state of the object.This reduces thepossibility of erroneous situations
Encapsulation • Hiding information within an object’s nucleus • Provide a public interface for interacting with it • Advantages to software developers Modularity: An object can be easily passed around in the system (Otherwise, you need to think about how many files you need to bundle together to pass to your friends) Information hiding: Users need not go into details of the object before using the object (E.g., you don’t need to know the circuit of a TV set if you want to watch TV) Safety: Users of the object may not directly access the internal state of the object. This reduces the possibility of erroneous situations. Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Library Reusability