Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.

Similar presentations


Presentation on theme: "Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is."— Presentation transcript:

1 Simple Classes

2 ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is problem dependent. e.g. A list –Data Name and height –Operations create empty lists add item remove item add ten feet to every height remove all records whose height is greater than 5 feet etc

3 C++ Classes Class –A structured data type used to represent ADTs Class member – a component of a class –data member –function (method)

4 Recall Structs Separates implementation of data from operations –Review Linked list demo last week Structs are said to be passive in the sense they just sit there and wait for something else to do something to them.

5 Classes Are active in the sense that the data and operations and bound together Data operates on itself and interacts with other data

6 Class types Like a struct a class is a programmer defined type. It is not a data object. You create or instantiate an object of a type. –Think of the class type like a stamp –and the objects are those things you stamp out.

7 Example struct Tdata { int age; double ht; }; struct Node { Tdata data; Node * next; }; typedef Node * NodePtr; class TList { public: void AddNode(int age, double ht); void CreateList(); void DisplayList(); void DeleteNode(int N); void DeleteList(); void DisplayNode(int N); private: NodePtr front; int nNodes; }; int main() { List A; return 0; } instantiate object A of type List

8 Public and Private If you look closely at the previous definition of the List class you may note one or two things class definition is similar to struct definition –In fact C++ treats structs as classes where by default all members are public class definition contains function prototypes –the operations are bound tightly to the data –class functions know about class data so you don’t need to pass class data to functions as arguments class definition has a line with a label public: this tells the compiler which parts of the class are visible to other things (clients) e.g main() class members by default are private. That is Private members functions and data are hidden to all. Private data and functions are only visible an object itself.

9 Class Data Manipulation The principle of Information hiding is the mantra for object oriented programming. Access to data is tightly controlled via public interface functions –“Setters” to set data members SetHeight(3); –“Getters” to obtain values of data members y = mylist.GetHeight();

10 Built in operators int, char, float, double have built in operators – + * / % etc –classes do not have them like structs they have. (dot) and = You have to define and implement your own operators –It is possible but beyond this course to redefine the built in operators to work on YOUR class.

11 Specification an Implementation Like ADT’s there are two parts to classes 1.the specification – declare the class 2.implement the function definitions

12 Usual Practice Put the class specification in a file –e.g. Tlist.h Put implementation in another file –e.g. Tlist.cpp Add both files to a project that wants to use a Tlist. Add the.h to the headers folder. –Now the project “knows” that there are more files to compile. Add a line #include “Tlist.h” to your main source file –Now main “knows” about Tlist classes DEMO 1

13 Specification file (.h) contains class and struct declarations contains class member function definitions implementation file (.cpp) RECALL DEMO 1

14 DEMO1 Shows practical elements of object oriented programming. –Information hiding Via class syntax Via implementation over multiple files Loose coupling –Classes are very self contained –There only interaction with other parts of a project is via its public interface which can be strictly monitored –They very independent to other parts of your project –They can therefore be developed and tested in isolation They result in more robust code They are easy to reuse/ adapt/extend/ –They are ideal for team programming For large project they are more productive –They are ideal for large programs

15 Constructors and Destructors Constructors guarantee initialisation We ALWAYS want to set up a list to use. Special member function called a constructor will “construct” or initialise any instance of our class exactly how we want. The constructor is called at the time of object instantiation!

16 Revised class declaration class TList { public: void AddNode(int age, double ht); //void CreateList();do not need this void DisplayList(); void DeleteNode(int N); void DeleteList(); void DisplayNode(int N); Tlist();//constructor ~Tlist()//destructor private: NodePtr front; int nNodes; };

17 Define constructor TList::TList() { cout << “constructing a list…” << endl; from = NULL; nNodes = 0; }

18 Define Destructor TList::~TList() { cout << "Desctructing a List..." << endl; DeleteList(); } DEMO 2 shows the active nature of classes The constructor is called the moment an object is created The destructor is called the moment the object is destroyed (goes out of scope).

19 Purpose of constructor and destructor They ensure that objects created are properly initialised They ensure that they are properly destroyed –So they can sort out any garbage collection, for example memory de-allocation. Once this is done properly then class can be used more reliably.

20 Summary A class is used to implement an ADT A class specifies members - the data and operations (methods) Information hiding is a central idea to classes class members are private by default Interface functions are explicitly labelled as public Classes provide automatic initialisation through constructors Classes provide a place for garbage collection through destructors. Creation of an object is achieved via a declaration statement.

21 Summary continued It is usual practice to declare and implement classes in separate files (specification and implementation) –Aids reuse –Aids team development

22 Further Ideas C++ and other object oriented languages take things further. For example Suppose we had a problem modelling vehicles We need an ADT for a vehicle

23 Vehicle ADT DATA: Position Speed heading Operation Accelerate Break Turn DisplayInfo

24 Suppose we want to model a bus A bus is a kind of vehicle It would be good if we could make use of out vehicle implementation and extend it to include bus specific data members and operations

25 Bus ADT bus is a kind of vehicle plus DATA: nPassengers Operations: board passenger alight passenger DisplayInfo

26 This can be done using inheritance A class can inherit data and operations from another class It can also redefine (polymorph) operations so that they work with the new class –e.g DisplayInfo will need to be adapted to display bus specific information as well as vehicle information. This process of inheritance further extends the programmers ability to reuse and extend code already written. These principles, lie at the heart of OOP –Information hiding –Inheritance and polymorphism

27 Object Oriented Programming Identify what the goals are for a problem Identifying the objects that make up a problem Identifying how the object in a problem interact with each other in order to satisfy the goals Specifying ADTs that describe the objects and operations needed for the problem

28 JAVA The second year module in JAVA takes these concepts further JAVA is a “pure” object oriented language with a distinclt C++ flavoured Syntax It is pure in the sense you cannot program in Java without classes.


Download ppt "Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is."

Similar presentations


Ads by Google