180 likes | 265 Views
CS-212. Intro to C++. Abstract Data Type. Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis on “what” not “how” Information Hiding to lower level of complexity Preconditions and Postconditions. Pre and Postconditions.
E N D
CS-212 Intro to C++
Abstract Data Type • Abstraction of a real world object as a mathematical entity • Implementation independent model of the entity • Emphasis on “what” not “how” • Information Hiding to lower level of complexity • Preconditions and Postconditions
Pre and Postconditions • Preconditions: the current state of the object • ex. The object exists and has a nonzero value • Postconditions: the new state of the object after the function has been performed • ex. The object still exists and has increased in value by a factor of ten • are described for every operation belonging to the object
C++ Class • An implementation of an ADT • A user defined “type” • Encapsulation of Data and Function • Data and its manipulators must always be kept together
Class • Consists of : • an interface definition (fn.h header file) • Data definition • Definition of user interface • an implementation (fn.cpp code file) • code for user interface • code for helper functions • Could both be in same file but that would defeat the purpose of information hiding
Class Interface Definition • class statement • class name • private: section • data content of the class • statements ahead of “public” are private • private functions are for use of the class not the class user • protected: section (for base classes only) • data content of the base class (more later...) • public: section • prototype statements for member functions • publishes the user interface to the class
Using a class • Must first instantiate (make an instance of) an object of type class (ex. throttle fuel_control) • Tell the object what you want it to do by using the “.” operator (dot operator). • fuel_control.shut_off() (note: specify the object and the action you want performed) • if the object returns a value assign it • current = fuel_control.flow();
Class Constructors • Assign initial values to an object • Never returns a value • Same name as the class • Guaranteed to be called (automatic) upon instantiation of an object of type class
Default Constructor • If no constructor is found • a default constructor, that does no initialization is used when the object is defined (only reserves the storage defined in the private section)
Object Initialization • a single constructor may be used that assigns values to all private variables • The constructor can be overloaded to provide flexibility to the user • each overload must have unique signature • Good idea to always provide a default constructor that assigns some value to all private variables
Destructors • Automatically run when an object of type class goes out of scope • use to clean up after the object (return dynamically allocated storage to the storage management system • Same name as the class, but preceded by a ~ • no overloading allowed • never returns a value • not needed for statically allocated storage
Member Functions • Also known as “setters” and getters” • provide the class’ behavior • you should be a “setter” function and a getter” function for each piece of private data (also known as “state data”)
Time class definition class Time { public: Time ( ) ; /* default constructor */ Time ( unsigned H , unsigned M , char AorP ) ; Time ( unsigned mil) ; Time (const Time & oTime ); /* copy constructor */ void SetHour(unsigned H ) ; void SetMinute ( unsigned M) ; void Setam_pm) (char C); void SetMiltime(); unsigned GetHour(); unsigned GetMinute(); unsigned Getam_pm (); unsigned GetMiltime() void Display ( ostream & out) ; private: unsigned hours , minutes ; char am_pm ; unsigned miltime ; }
Time class implementation Time::Time() // Constructor for the Time class { // initialize time to midnight hours = 0 ; minutes = 0; am_pm = “A”; miltime = 0; } Time::Time ( unsigned H , unsigned M , char AorP ) ; { hours = H ; minutes = M am_pm = AorP; if (am_pm ==“P”) miltime = ((hours+12)*100)+minutes; else miltime = hours*100 + minutes; }
Time class implementation Time:: Time ( unsigned mil) { hours = mil / 100 if (hours > 12) { hours = hours - 12; am_pm = “P”; } else am_pm = “A”; minutes = mil % 100; miltime = mil; }
Time Class Copy Constructor Time::Time(const Time & oTime) { hours = oTime.hours; min = oTime.min; am_pm = oTime.am_pm; miltime = oHours.miltime; }
Inline Member Functions • If a member function is simple; define it inline in the class definition (header file) (provide the function body {.....} inline) • Compiler recompiles the inline code each time it is called and places the code inline (like a C macro) • faster code • uses more storage • Functions may also be “inlined” by using the Inline keyword
namespaces • using namespace std; • The concept of namespaces was added to C++ in the last release of the specification • namespaces allow you to assign portions of your program to different namspaces, variable names are local so they may be reused with in a program as long as the uses are in different namespaces • the namespace “std” is a little special • you don’t have to specify the “.h” for all standard #includes • #include <iostream>