CS3505 Lecture3
CS3505 Lecture3
Lecture 3:
Object-Oriented Programming in
C++
Assignment
• Some good questions and discussion on Piazza
(Some summaries and supporting assignment text)
– How do I make compile.txt and what is it?
• Cut-and-paste your compilation command into a text file
“Save the compilation command in a text file called compile.txt.”
– My populations are real close to the expected so good
enough?
• Compute changes in both populations before updating
“compute the change in population and then update R and F by
adding deltaRabbit and deltaFoxes respectively”
– The autograder says I have a wrong “\n” at the end
• The assignment asks you to add newlines elsewhere
“This function should not add a newline character to the bar chart
line”
C++ Classes
• Why object-oriented programming?
• Major principles
– Encapsulation
– Abstraction
– Inheritance
– Polymorphism
Goal of Object-Oriented Programming
• Struct
– fails abstraction – internal representation has
to be known
– fails encapsulation – internals are exposed
C++ Class Basics
• Keyword class
• Data members (instance variables)
• Member functions - methods
• Constructor
}; // don’t forget ;
newpt = pt * 2.0;
becomes
newpt = pt.operator*(2.0);
Once you know that, defining them is easy
An Overloaded *
• One way
Point operator*(double scaleFactor) {
Point res;
res.x = x * scaleFactor;
res.y = y * scaleFactor;
return res;
};
• Another
Point res(x * scaleFactor, y * scaleFactor);
return res;
Overload <<
• Want
cout << myPoint << endl;
this.update(); this->update();