140 likes | 152 Views
Structures Revisited. what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure? what are structures used for? is a structure definition an executable statement? Can it be put in a header file?
E N D
Structures Revisited • what is an aggregate construct? What aggregate constructs have we studied? • what is a structure? what is the keyword to define a structure? • what are structures used for? • is a structure definition an executable statement? Can it be put in a header file? • why includeing a header file with structures definition multiple times is a problem? how is this problem solved? • what are #define #ifndef #endif? • what is a structure variable? • what is the term for elements of a structure? • do elements of the same structure (different structures) have to have unique names? • how can structures be initialized? what happens when one structure variable is assigned the value of another? • can structure variables be directly compared? passed as parameters? by value? by reference? can a function return a structure? • can one structure be included in another structure? What is a substructure? • can a structure include an array? how can an array of structures be declared?
What’s Wrong with Structures? • structure is an aggregate construct providing encapsulation • encapsulation - combining a number of items in a single entity • consider implementing date: • as structure: struct Date{ int month; int day; int year; }; • and a set of functions manipulating dates: void set_date(Date &d, int, int, int); void add_year(Date &d, int n); bool compare(Date &d1, Date &d2); • problems: • there is no explicit connection between data type (structure) and these functions • it does not specify that the functions listed should be the only ones that access and modify the date variables • if there is a bug in Date manipulation - it can be anywhere in the program • if modification of Date is needed – the whole program needs to be updated
Class Definition • class is an aggregate data type • a class may contain member variables (attributes) and member functions (methods) • member variables and member function prototypes are declared within the class definition • member functions can manipulate member variables without accepting them as parameters class Date { // class name public: // ignore this for now void set(int, int, int); int getday(void); int month; int day; int year; }; // don’t forget the semicolon • a variable of type class is called an object (how is a variable of type structure called?) Date mybday; • the object is said to belong to the class or be an instance of the class • each object has member variables and can call member functions of its class. Addressing the members is done using dot-operator: mybday.set(10, 26, 68); cout << mybday.day;
Public and Private Members • public/private attributes control the way the class members are accessed (why do we want to do that?) • public members can be accessed within member functions (no scope resolution needed) and outside (with dot operator) • private member - can only be accessed within member functions class Date { // class name public: void set(int, int, int); int getday(void); private: int month; int day; int year; }; // don’t forget semicolon • make member variables private, make functions either public or private. This restricts manipulation of variables to member functions which makes debugging and changes in a class easier • examples: mybday.set(10, 5, 99); // good mybday.year=99; // ERROR - private member
Member Function Definitions • functions can be defined either inside or outside the class definition • outside definition - class name (called type qualifier) and scope resolution operator (::) precedes the function name: void Date::set(int m, int d, int y){ month=m; // no dot with member variables day=d; // no declaration of member variables year=y; } • to define inside - replace prototype with definition class Date { public: void set(int, int, int); int getday(void){return(day);}; //still needs “;” private: int month; int day; int year; }; // don’t forget semicolon • a function defined inside the class definition is called an in-line function - at compilation the function code replaces every invocation - use only for small functions
Mutators and Accessors • accessor function – a member function that does not modify the state of an object (only returns the information about the object’s state) • mutator function – a member function that modifies the object’s state • accessors should be marked with the const type modifier so that the compiler can find accidental object modificationalso remember to use const with parameters that are not modified class Date { public: // mutator void set(int, int, int); // mutator int getmonth(void) const; // accessor // accessor in-line int getday(void) const {return(day);}; private: int month; int day; int year; }; • separate mutators and accessors - a function should not do both • In many situations (but not all) private variables will need an accessor function. • It also better to keep variables private, but that is not a universal rule either. • are we missing an accessor in Date?
Constructors • constructor – a mutator that is invoked automatically when the object is declared • used to assign an initial state to the object (initialize object) • a constructor does not have a return value and it has the same name as the class, do NOT put void as a return value of the constructor class Date { public: Date(int, int, int); // constructor private: int month, day, year; }; • outside constructor definition (can also be inlined) Date::Date(int m, int d, int y){ month=m; day=d; year=y; } constructor is invoked at declaration:Date mybday(10,26,99); or directly: yourbday=Date(10,26,00);
Multiple/Void Constructors • A class can have multiple constructors • The constructor called is determined by the number and type of arguments • function overloading – same name functions distinguished by number/type of arguments • it is bad style to define constructors that differ by type of arguments only - confusing • if no constructors are declared - objects can be defined without initialization • void constructor - a constructor that does not take arguments • caveat: if at least one constructor is declared - object has to always be initialized (needs a void constructor) class Date { public: Date(int, int, int); // constructor Date(int, int); // another constructor Date(void); // void constructor private: int month; int day; int year; }; • calling default constructor (if constructors defined): Date mybday; • if array of objects, default constructor is invoked Datefriendsbday[20]; • what is this? Date mybday();
Constructors for Member Objects • (void) constructors for member objects are invoked before the constructor for the containing object • example class one{ public: one(void) { cout << ”one's constructor" << endl; }; }; class two{ public: two(void) { cout << ”two's constructor" << endl; }; private: one o; }; int main(void) { two ob2; } • prints one’s constructortwo’s constructor • note, if containing object does not have a constructor, member object (void) constructors are still invoked
Friend Functions • to allow a non-member function to access private class members it should be declared as a friend function: class Date { public: // compares two dates friend bool equal(const Date&, const Date&); private: int month; int day; int year; }; • a friend-function is not a class member - it’s definition should not be preceded with type qualifier (Date::) • no separate prototype (outside class definition) is necessary for friend • equal is invoked as a non-member function - without dot operator: if(equal(date1, date2)) { ... • friend-functions circumvent information hiding and should be used as little as possible and with great care • use member function if the task to be done involves only one object • only use friend-function if the task requires access to private members of more than one object and the objects are used symmetrically • you can almost always use accessors instead of friends
Static Constants • the scope of a static constant is the class: it is available to all methods • it is stored only once but every object of this class has access to it • can be private or public class Date { public: ... private: static const int CENT21 = 2001; ... }; • variables can also be declared static. Careful with those since they are in effect global variables (not as problematic, why?)
Program Layout • program with objects is laid out as follows: • header - class definition (inline function definitions), global constants and other non-executable constructs related to a class • program file - member function definitions • multiple related classes may be put in one header/program file pair
Classes Review • What is a class? Why are classes needed? • What is an object? • What is the difference between an object and a class? What do we mean when we say an object belongs to a class? • What is a member variable? Member function? Method? Attribute? • What is an in-line function definition? • What are these operators used for? “.” “::” • What is the difference between public and private members of a class? • What is the state of an object? • What is a mutator? Accessor? How is an accessor function distinguished? • What is a constructor? Is a constructor a mutator or an accessor? • What is the name of a constructor? • How many parameters can a constructor take? Can there be more than one constructor? What is constructor overloading? What is a void constructor? • What is the return value of a constructor? • What is a friend function? Why is it needed? • What are static attributes?