Intro to C++ January 6, 2014 - structs, classes, data members, const

4 Pages
106 Views
Unlock Document

Department
Electrical and Comp Engineering
Course
EECE 2560
Professor
Waleed Meleis
Semester
Spring

Description
Introduction to C++ January 6, 2014 Structs % contain data struct Rectangle { int height; % member int weidth; % member }; Rectangle r1; cin >> r1.height; cout << r1.width; Classes Contain data and functions class time24 { public: time24(int h=0, int m = 0); void addTime (int m); // a function that doesn’t return anything is void time24 duration (const time24 &t); // t is a reference to a time24 object that is a const. a parameter is const void readTime(); void writeTime() const; // a member function is const, which means the data member cannot be changed. int getHour() const; int getMinute() const; private: int hour; % data member int minute; % data member void normalizeTime (); %data member }; public members (function and data) are accessible anywhere a time24 object is accessible. —> the externally facing view of this object private members are only accessible to members of the class —> why shield “inward- facing” components of the code from the users of the class? Code can be extremely complex, sometimes we don’t want data/information changed within the code encapsulation - grouping of functions and data in an object information hiding - keeping the private members from users of the class int main () { time24 time1; time24 time2; time1.readTime(); time2.readTime(); time1.writeTime(); time2.writeTime(); time24 time3; time3 = time1.duration(time2); %version of “duration” that applies to time1 } void time24::normalizeTime() { int extraHours = minute/60; // scope of minute —> class // scope of extraHours — > normalizeTime() minute = minute % 60
More Less

Related notes for EECE 2560

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit