Constructors and Accessor Functions Lecture 10 Outline What

  • Slides: 20
Download presentation
Constructors and Accessor Functions Lecture 10

Constructors and Accessor Functions Lecture 10

Outline

Outline

What is a constructor? ¨ It is a member function which initializes a class.

What is a constructor? ¨ It is a member function which initializes a class. ¨ A constructor has: (i) the same name as the class itself (ii) no return type

class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float,

class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor void draw(); // member function void posn(int, int); // member function void move(int, int); //member function }; rectangle: : rectangle(float h, float w) { height = h; width = w; xpos = 0; ypos = 0; }

Comments on constructors ¨ A constructor is called automatically whenever a new instance of

Comments on constructors ¨ A constructor is called automatically whenever a new instance of a class is created. ¨ You must supply the arguments to the constructor when a new instance is created. ¨ If you do not specify a constructor, the compiler generates a default constructor for you (expects no parameters and has an empty body).

Comments on constructors (cont. ) void main() { rectangle rc(3. 0, 2. 0); rc.

Comments on constructors (cont. ) void main() { rectangle rc(3. 0, 2. 0); rc. posn(100, 100); rc. draw(); rc. move(50, 50); rc. draw(); } ¨ Warning: attempting to initialize a data member of a class explicitly in the class definition is a syntax error.

Overloading constructors ¨ You can have more than one constructor in a class, as

Overloading constructors ¨ You can have more than one constructor in a class, as long as each has a different list of arguments. class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(); // another constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

Overloading constructors (cont. ) rectangle: : rectangle() { height = 10; width = 10;

Overloading constructors (cont. ) rectangle: : rectangle() { height = 10; width = 10; xpos = 0; ypos = 0; } void main() { rectangle rc 1(3. 0, 2. 0); rectangle rc 2(); rc 1. draw(); rc 2. draw(); }

Classes & ADTs ¨ Accessor Functions – Functions that give you access to the

Classes & ADTs ¨ Accessor Functions – Functions that give you access to the values of the private member variables. class School { public: … private: int Num. Of. Students; int Num. Of Classes; double Area; } int get_Students(); //Return the number of students in a school int get_Classes(); //Return the number of classes in a school double get_Area(); //Return the area of a school

Classes & ADTs ¨ Private members need for Accessor Functions int get_id(); //returns the

Classes & ADTs ¨ Private members need for Accessor Functions int get_id(); //returns the student id n n n char get_major(); //returns the student major void set_id(int new_id); //assigns a value to student id void set_major(char new_major); //assigns a value to student major Student ID Major Student { public: int id; char major; };

Classes & ADTs ¨ Can we overload member functions? – void set(int the_id, char

Classes & ADTs ¨ Can we overload member functions? – void set(int the_id, char the_major[2]); – void set(int the_id); – void set(double score); Student new_student; new_student. set (16. 0); new_student. set (555); New_student. set ((999, ”CS”); Search for matching data types and/or number of parameters

Classes & ADTs Class Day. Of. Year { public: void output(); private: int month;

Classes & ADTs Class Day. Of. Year { public: void output(); private: int month; int day; }; Restriction: Once you make a member variable private, the only way to access it or change its value is by using one of the member functions. int main() { Day. Of. Year Today; cin >> Today. month; cout << Today. month; If (Today. month ==1) cout << “January”; private member variables ILLEGAL!

Classes & ADTs Class Sample { public: int variable; void output(); void input(); private:

Classes & ADTs Class Sample { public: int variable; void output(); void input(); private: int month; int day; void do. Stuff(); }; public members private members Public members can be used in the main body of your program or Public members in the definition of any function, even a non- member function.

//Program to demonstrate the class Bank. Account. #include <iostream> using namespace std; //Class for

//Program to demonstrate the class Bank. Account. #include <iostream> using namespace std; //Class for a bank account: class Bank. Account{ public: void set(int dollars, int cents, double rate); //Postcondition: The account balance has been set to $dollars. cents; //The interest rate has been set to rate percent. void set(int dollars, double rate); //Postcondition: The account balance has been set to $dollars. 00. //The interest rate has been set to rate percent. void update( ); //Postcondition: One year of simple interest has been //added to the account balance. double get_balance( ); //Returns the current account balance. double get_rate( ); //Returns the current account interest rate as a percent.

void output(void); //Postcondition: Account balance and interest rate have been display to the //screen.

void output(void); //Postcondition: Account balance and interest rate have been display to the //screen. private: double balance; double interest_rate; double fraction(double percent); //Converts a percent to a fraction. For example, fraction(50. 3) returns 0. 503. }; int main( ) { Bank. Account account 1, account 2; cout << "Start of Test: n"; account 1. set(123, 99, 3. 0); cout << "account 1 initial statement: n"; account 1. output();

account 1. set(100, 5. 0); cout << "account 1 with new setup: n"; account

account 1. set(100, 5. 0); cout << "account 1 with new setup: n"; account 1. output(); account 1. update( ); cout << "account 1 after update: n"; account 1. output(); account 2 = account 1; cout << "account 2: n"; account 2. output(); return 0; } void Bank. Account: : set(int dollars, int cents, double rate) { if ((dollars < 0) || (cents < 0) || (rate < 0)) { cout << "Illegal values for money or interest rate. n"; exit(1); }

balance = dollars + 0. 01*cents; interest_rate = rate; } void Bank. Account: :

balance = dollars + 0. 01*cents; interest_rate = rate; } void Bank. Account: : set(int dollars, double rate) { if ((dollars < 0) || (rate < 0)) { cout << "Illegal values for money or interest rate. n"; exit(1); } balance = dollars; interest_rate = rate; } void Bank. Account: : update( ) { balance = balance + fraction(interest_rate)*balance; }

double Bank. Account: : fraction(double percent_value) { return (percent_value/100. 0); } double Bank. Account:

double Bank. Account: : fraction(double percent_value) { return (percent_value/100. 0); } double Bank. Account: : get_balance( ) { return balance; } double Bank. Account: : get_rate( ) { return interest_rate; } //Uses iostream: void Bank. Account: : output(void){ cout. setf(ios: : fixed); cout. setf(ios: : showpoint); cout. precision(2); cout << "Account balance $" << balance << endl; cout << "Interest rate " << interest_rate << "%" << endl; }

Thank You

Thank You