CS 162 Intro to CS II Structs vs. Classes 1
Odds and Ends Assignment 1 questions Why does the delete_info have a double pointer to states as a parameter? Do your functions have to be 15 or under? Anymore??? 2
Structs vs. Classes Differences Similarities 3
Class vs. Object Class declaration/definition is type. Object is an instance of a class. Example: int x; int y; ; int main() { Point p1, p2; p1.x=10; p1.y=20; p2.x=5; p2.y=6; return 0; 4
Class w/ Behavior/Member Functions int x; int y; void translate(int dx, int dy); //Translates to a new x, y location given distance ; int main () { Point p1, p2; p1.x=10; p1.y=20; p2.x=5; p2.y=6; p1.translate(-1, 3); p2.translate(2, -2); return 0; void Point::translate(int dx, int dy) { x += dx; y += dy; 5
Can we set the values for x and y? int x = 0; //This is allowed in C++ 11!!! int y = 0; // This is allowed in C++ 11!!! void translate(int dx, int dy); //Translates to a new x, y location given distance ; int main () { Point p1, p2; p1.x=10; p1.y=20; p2.x=5; p2.y=6; p1.translate(-1, 3); p2.translate(2, -2); return 0; void Point::translate(int dx, int dy) { x += dx; y += dy; 6
What if we made states private? void translate(int dx, int dy); private: int x; int y; ; int main () { Point p1, p2; p1.x=10; p1.y=20; p2.x=5; p2.y=6; //This is not allowed!!! //This is not allowed!!! p1.translate(-1, 3); p2.translate(2, -2); return 0; void Point::translate(int dx, int dy) { x += dx; y += dy; 7
Encapsulation/ADTs Why do this? How do we set/get private member variables? Accessor and Mutator Functions Access: get Mutator: set 8
Encapsulation void set_xy(int thex, int they); //Mutator Function private: int x; int y; ; int main () { Point p1, p2; p1.set_xy(1, 1); p2. set_xy(2, 2); return 0; void Point::set_xy(int thex, int they) { x = thex; y = they; 9
How do we write an Accessor Function? void set_xy(int thex, int they); //Mutator Function int get_x(); //Accessor Function int get_y(); //Accessor Function private: ; int x; int y; int main () { Point p1; p1.set_xy(1, 1); std::cout << p1.get_x(); << \t << p1.get_y() << \n ; return 0; int Point::get_x() { return x; int Point::get_y() { return y; void Point::set_xy(int thex, int they) { 10
Constructors Point(int x_val, int y_val); //Constructor void set_xy(int thex, int they); //Mutator Function int get_y(); //Accessor Function int get_x(); //Accessor Function private: int x; int y; ; int main () { Point p1(1, 1), p2(2, 2); p1.point(1, 1); //This is illegal p2.point(2, 2); //This is illegal return 0; Point::Point(int x_val, int y_val) { x=x_val; y=y_val; 11
Constructors Point(int x_val, int y_val); //Constructor void set_xy(int thex, int they); //Mutator Function int get_y(); //Accessor Function int get_x(); //Accessor Function private: int x; int y; ; int main () { Point p1, p2; //Calls the default constructor but we don t have one!!! return 0; Point::Point(int x_val, int y_val) { x=x_val; y=y_val; 12
Constructors Point(); Point(int x_val, int y_val); //Constructor void set_xy(int thex, int they); //Mutator Function int get_y(); //Accessor Function int get_x(); //Accessor Function private: int x; int y; ; int main () { Point p1, p2; p1=point(1, 1); p2=point(2, 2); return 0; Point::Point(int x_val, int y_val) { x=x_val; y=y_val; Point::Point() { x=0; y=0; 13
Another way to define Constructors Point(int x_val, int y_val); //Constructor void set_xy(int thex, int they); //Mutator Function int get_y(); //Accessor Function int get_x(); //Accessor Function private: int x; int y; ; int main () { Point p1(1, 1), p2(2, 2); return 0; Point::Point(int x_val, int y_val) : x(x_val), y(y_val) { /*Do nothing in here*/ 14
More defining Constructors Point(int x_val, int y_val); //Constructor void set_xy(int thex, int they); //Mutator Function int get_y(); //Accessor Function int get_x(); //Accessor Function private: ; int x; int y; int main () { Point p1(1, 1), p2(2, 2); return 0; Point::Point(int x_val, int y_val) { : x(x_val), y(y_val) if(x < 0 y < 0) std::cout << You need to enter a positive number << std::endl; 15
Write our own string class 16
17
18
Class Type Member Point(); //Default Constructor private: int x; int y; ; class Points { Point p; ; int main() { Points pts; cout << pts.p.get_x(); return 0; 19
Class Type Member Point(); //Default Constructor private: int x; int y; ; class Points { Points(); //Default Constructor private: Point p; ; Points::Points() : p() { 20
Class Type Member Point(); //Default Constructor private: int x; int y; ; class Points { Point p[10]; ; int main() { Points pts; cout << pts.p[0].get_x(); return 0; 21
Making Point private 22
Passing Objects, what s wrong 23
Passing Objects, the fix 24
What is const vs. static? What is const? const int x; //cannot have as member var void function(const int &x) { void function() const { When would we want to make a member function const? When wouldn t we? What is static? Class variable or function static int x;. Point::x Can have a static const int x=0; 25
Odds and Ends Exercise #1 due Friday Sign up for a study session on Exercises page Now, get out a piece of paper to take a pretest, which will be counted as part of Exam I 26
Happy April Fools!!!!! LOL!!! Got you! 27