Download presentation
Presentation is loading. Please wait.
1
Classes and Objects April 6, 2011
2
Object Oriented Programming Creating functions helps to make code that we can reuse. When programs get large it becomes difficult to manage so many functions. functions are organized into objects. Objects are more intuitive to use.
3
Objects in Programs You already know about the ifstream class Objects of this type can do the following functions: close(), fail(), clear() etc.
4
Encapsulation You use the functions from the ifstream, like close(), without knowing the details of the implementation. Easier not to have to understand how the code looks. This hiding is called encapsulation. When you create an object from scratch you should make it easy to use w.o. knowing the details of the methods.
5
Specify the Public Interface How the class interacts with a programmer is called the interface. The functions you can call. Example: I want a class to represent a rectangle. I want the interface to allow the programmer to find the area. I also want him to be able to find the perimeter. I will also add some functions to allow the user to set the length and width to desired values.
6
Interface Design Rectangle public: setLength(double l); setWidth(double w); findArea( ); findPerimeter( );
7
Member Variables In order to find the rectangle’s area and perimeter, the rectangle will need to know its length and width. Outside users don’t need to know the information. It is private.
8
Instance Variables Added Class Design Rectangle public setLength(double l); setWidth(double w); findArea( ) findPerimeter( ) private double length double width
9
Implementing the class. Now that I have a design, I can implement the class. rectangle.cpp Need to put private instance variables in the class.
10
Implementing the class. Rectangle Finish the functions, using the data members to compute the measurements. Once I finish writing the class, I need to create an object so that I can test the class. I’ll write a main method to create a rectangle or two.
11
Class Definition Syntax class Sample { public: fnc_prototype(); fnc_prototype2(); //... etc. private: member_var1; member_var2; // … etc. };
12
Member Function Definition return_type class_name :: function_name(parmlist) { statements } :: is scope resolution operator
13
Calling a function Object_name.function_name(arg list); e.g. High.input_T( );
14
Scope of data members Can use data members in any of the functions in the class. There is no need to pass them to functions.
15
Constructor Has the same name as the class. Is executed automatically when a new object is made. Can be passed some values to initialize the instance variables. Now, I am ready to instantiate or create an object
16
Another Example – a Car Class Design and Implement a Car class. A Car should be able to tell you how far it can go on the gasoline left in the tank. It should also be able to go “Vroom!”. What will the interface look like? What data members will it have? How will its constructor look? What should a main() method to test it do?
17
Another Class
18
Sphere class Design and implement a class called Sphere that contains member data that represents the sphere’s diameter. Define a Sphere constructor to accept and initialize the diameter. Include functions to return the volume and the surface area of the Sphere. Write a main function to create and test a Sphere.
19
Accessor Function A function that allows the main() function to see a private data member. Usually starts with “get” Return the private data. I’ll add a getDiameter() to the Sphere class.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.