270 likes | 414 Views
Classes - Part II. Constructors Member Functions Inheritance. Classes. Structured Programming 256 Chapter 9. Class Implementation. A constructor is a special member function provided to allow initialization of variables. Constructors.
E N D
Classes - Part II Constructors Member Functions Inheritance
Classes Structured Programming 256 Chapter 9
Class Implementation • A constructor is a special member function provided to allow initialization of variables.
Constructors • constructor initializes private data when a class object is created • ExampleTimeType(int, int, int); • Student(double, char, char, char, char);
Default Constructors • default constructor parameterless constructor, it initializes the variables to some default value, such as zero • ExampleTimeType(); • Student();
same Default Constructor Function Syntax Classname::Classname() { function body } *
Constructor Functions • do NOT have a return type (not even void) • must have the same name as the class itself • invoked when you declare an instance of a class with a list of initial values • there should be a default constructor * * * *
Default Constructor Function • Example 1 • TimeType::TimeType( ) • { • hrs = 0; • mins = 0; • secs = 0; • } Example 2 Student::Student( ) { GPA = 0; num_of_grades = 130; } * *
Default Constructor Function Example 3 Circle::Circle( radius) { radius_of circle = radius; x_center = 5; y_center = -3; } Example 4 Student::Student( ) { } * *
Destructors • ~Classname( ) • A default do-nothing destructor is provided by the compiler. • Only one destructor per function class • No arguments - no return values
Some Terminology • OOP C++ • object class object or class instance • instance variable private data member • method public member function • message passing function call (to a public member function)
h file Review - In Specification File • default constructorClassname();Track(); • constructorClassname(typepara, typepara , ...); Track(int disc, double hurdles); • member functiontype Function(typepara , typepara , ...);void Event(double hurdles); * * *
Review - In Implementation File default constructorClassname::Classname() { } Track::Track() { } • constructorClassname::Classname(typepara, typepara , ...) Track::Track(int disc, double hurdles) • member functiontype Function(typepara , typepara , ...)void Event(double hurdles) all have a body * *
Review - In Client File • default constructor If NOT passed parameters - gets called when declaring an object of type Class. • constructor If passed parameters - gets called when declaring an object of type Class. • member functionobject.Function(para , para , ...);b_jenner.Event(double hurdles); * * *
Inheritance • creating new classes out of existing ones • reuse code without need for retesting and validation • speeds up programming process • simple vs. multiple
Inheritance - simple Circle base class: derived class: derived class: Sphere Cylinder *
Inheritance - multiple base classes: derived class: Car Truck Minivan *
Constructor Function Call • SyntaxClassnameobject(arguments); • orClassnameobject = Classname(arguments); * *
} Constructor Function Call • Example Circle big_circ(0, 2. 3.6); Circle big_circ = Circle(2, 2, 3.6); • Circle big_circ( ); Circle big_circ; * * * *
Overload Constructors Date me; Date you(7,25,49); Date moon(690628); • You must provide constructors for each. • PrototypesHeaders • Date( ); Date::Date() • Date(int, int, int); Date::Date(int, int, int) • Date(long) ; Date::Date(long)
Class Implementation:Function Syntax • Exampledouble Circle::setVal(int xx, int yy, double rr) • { xcenter = xx; ycenter = yy; radius = rr; } * *
Function Call • Syntax object.function(arguments); Examplea.setVal( ); b.setVal(3, 2, 6.5); big_circ.setVal(1, 4, 8.3) sm_circ.setVal( ); k.setVal(1, 0, 5.0); * *
Initialize Data Membersof a Class • double checkAmt; • checkAmt = 54.78; Student iago; iago.ID = 12345; * *
Arrays of Objects • Date theDate[4]; • creates the objects: • theDate[0] • theDate[1] • theDate[2] • theDate[3]
Arrays of Objects • Function calls: • thedate[0].showdata(); • or • for(ndx=0; ndx<4; ndx++) • theDate[ndx].showdata();
Common Errors • missing ; at end of class declaration • including a return type with constructor’s prototype • missing a return type with function prototypes • defining more than one default constructor per class
Common Errors • missing :: and class name in header • forgetting to include the appropriate header files