Download presentation
Presentation is loading. Please wait.
Published byMiles Duane Neal Modified over 8 years ago
1
Matthew Small small@cs.fsu.edu Introduction to Object-Oriented Programming
2
Object-oriented Basics Object-oriented programming languages allow a programmer to combine data and functions that act on that data into a single entity called an object. An object consists of: Name – a way of referring to an object inside a program. Member Data – data contained within an object. Member Functions – routines that act upon the data within an object. Interface – defines the ways a programmer may directly access the member data/functions of an object
3
Class A class is the blueprint for an object A class outlines member data, member functions, an interface and a class name to refer to it. A programmer can create one or more objects of a given class. Similar to building multiple houses from a single blueprint.
4
Interface An interface is specified in a class and affects the access a programmer has to objects of that class. At least two levels of protection can be specified within a class: public – member data/functions that are accessible both inside and outside the object private – member data/functions that are only accessible from the member functions of the object The public members of a class provide an interface for objects The private members of a class provide internal functionality and data representation for objects By convention, member data is kept private
5
Reasons for protection levels Makes interface simpler for programmer. More secure. Less chance for misuse (accidental or malicious). Class implementation easy to change without affecting other modules that use it.
6
How to Incorporate Objects DDU – declare, define, use Declare class choose class name, interface (protection levels) and member variables/functions without providing an implementation for member functions (function body) outlines the class functionality Define member functions write code that implements member functions completes class specification Use class to create object(s) use the specification provided by the class to create an object use object interface to interact with object
7
Class Declaration Format class { public: //members accessible externally and internally private: //members only accessible internally }; //remember to put ‘ ; ’ here
8
Example Class Declaration class Circle { public: void SetRadius(double r); //sets member variable radius to r double AreaOf(); // returns area of circle as a double private: double radius; // radius of circle };
9
Defining Member Functions There are two ways to provide the member function definitions for a class inside the class declaration using {} (we will not use) after the class declaration (this is the method we choose) How to refer to a member function: className::memberFuntionName this identifier refers to the member function memberFunctionName of class className (e.g. Circle::SetRadius) The double colon :: is called the scope resolution operator After the class declaration, member functions are defined just like any other function
10
Example Member Function Definition //Declaration: class Circle { public: void SetRadius(double r); // sets member variable radius to r double AreaOf(); // returns area of circle as a double private: double radius; // radius of circle }; //Definition (Implementation): void Circle::SetRadius(double r) { radius = r; // radius refers to this object’s member variable 'radius' } double Circle::AreaOf() { return (3.14*radius*radius); }
11
Object Use After a class has been declared and defined, an object of that class can be be declared (also known as creation or instantiation) and used. A programmer can declare an object with the following format: ClassName ObjectName; This statement creates an object based on the blueprint of class ‘ClassName’ and the object can be referred to by the identifier (variable name) ‘ObjectName’
12
Accessing an Object’s Public Members The ‘. ’ (dot) operator can be used to access an object’s public members The format for referring to an object’s member is: ObjectName.MemberFunction() OR ObjectName.MemberVariable Remember that this only works for public members of an object and since, by convention, member data is kept private, we will not use the second format.
13
Putting it All Together See sample1.cpp To recap, this program: declares the class Circle and outlines its members and interface defines the implementation for the member functions of the Circle class declares two objects of the class Circle, referred to as C1 and C2 uses the interfaces of C1 and C2 to store the radius of two circles and later to calculate the area of those circles
14
Review What is a class? What is an object? How do we define the interface in a class? Where can public members be accessed? Private members? What is the difference between a declaration and a definition?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.