EECE3326_Jan8_2014 - Copying and Passing Objects

3 Pages
Unlock Document

Northeastern University
Electrical and Comp Engineering
EECE 2560
Waleed Meleis

Introduction to C++ 1/8 Inline Code When functions are only one line or two, sometimes you don’t need to write a dedicated funciton for it. You can put the code directly into the class declaration. int time24::getHour() const { return hour; } t1.getHour() ___________ class time24 { public: int getHour const { return Hour } …… private: …… } t1.getHour() Copying and Passing Objects class Count { public: Const (int); void pnt() { count << r << endl;} void set (int); private: } int x; Countt::Count(int val) { set (val); } void Count::set(int val) { x = val; } int main() { Count counter1(5) Count counter2(20) counter1.print(); Clear(counter1); counter1.print(); % should clear to 0 } void Clear(Const aCounter) %wrong way to implement. Will not permamently change value of counter1 when we call it in main (above) { aCounter.set(0); } //When you pass an object by value, the object is copied. Changes aren’t visible outside the function —> Default memberwise copying happens. //We want to pass a value by reference. void Clear (Const &aCounter) //&aCounter is an alias for the object passed in //When we pass by reference, changes to objects are visible outside the function… no copy is made { aCounter.set(0); } ______ Const Objects can’t be changed Const member functions - don’t change any data members within other objects ______ class Increment { public: Increment (int c = 0, int i) //initial count = 0, incremement value = i void addInc
More Less

Related notes for EECE 2560

Log In


Don't have an account?

Join OneClass

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

Sign up

Join to view


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.