CS 162 Intro to CS II. Structs vs. Classes

Similar documents
PIC 10A. Lecture 15: User Defined Classes

Package. A package is a set of related classes Syntax to put a class into a package: Two rules: Example:

The Class Construct Part 2

CS 11 C++ track: lecture 1

CS11 Intro C++ Spring 2018 Lecture 1

Scope. Scope is such an important thing that we ll review what we know about scope now:

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

C++ Constructor Insanity

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Building Java Programs

CS11 Intro C++ Spring 2018 Lecture 3

CS11 Introduction to C++ Fall Lecture 1

Where do we stand on inheritance?

System Programming. Practical Session 9. C++ classes

Abstract Data Types (ADT) and C++ Classes

What is an algorithm?

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

CS170 Introduction to Computer Science Midterm 2

2015 Academic Challenge

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS

CS250 Final Review Questions

Lecture 12: Classes II

Question 1. [5 points] Consider the following partially-complete program, which begins on the left and continues on the right: HERE

Comp151. Inheritance: Initialization & Substitution Principle

pointers & references

Comp151. Inheritance: Initialization & Substitution Principle

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Admin. CS 112 Introduction to Programming. Exercise: Gene Finding. Language Organizing Structure. Gene Finding. Foundational Programming Concepts

Namespaces and Class Hierarchies

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Building Java Programs

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Lecture 11: Intro to Classes

Chapter 7. Constructors and Other Tools. Copyright 2016 Pearson, Inc. All rights reserved.

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

CS24 Week 4 Lecture 1

BBM 102 Introduc0on to Programming II Spring 2014

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Programming Languages and Techniques (CIS120)

Object-Oriented Programming

Building Java Programs

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

C++ (classes) Hwansoo Han

Constants, References

CS 115 Exam 3, Spring 2011

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

CMSC 341 Lecture 7 Lists

CS 112 Introduction to Programming

Encapsulation in C++

CS 170 Java Programming 1. Week 12: Creating Your Own Types

CS11 Introduction to C++ Fall Lecture 3

CS250 Final Review Questions

Chapter 19 - C++ Inheritance

CS18000: Programming I

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

! Theoretical examples: ! Examples from Java: ! data type: A category of data values. " Example: integer, real number, string

Building Java Programs Chapter 8

A programming problem. Building Java Programs Chapter 8. Observations. A bad solution. Classes

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

An Introduction to C++

Initializing and Finalizing Objects

CS 455 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

Programming, numerics and optimization

SFU CMPT Topic: Classes

Review Questions for Final Exam

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

An introduction to Java II

CIS 190: C/C++ Programming. Classes in C++

Class CSE F. *slides are from CSE S at SKKU & at MIT

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CSE 373. Objects in Collections: Object; equals; compareto; mutability. slides created by Marty Stepp

Miri Ben-Nissan (Kopel) (2017)

CSCI-1200 Data Structures Spring 2018 Exam 1 Solutions

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

A Unit Testing Framework for Aspects without Weaving

7. (2 pts) str( str( b ) ) str '4' will not compile (single, double, or triple quotes

CSC102 INTRO TO PROGRAMMING WITH PYTHON LECTURE 28 OOPS IN C++ MICHAEL GROSSBERG

CS250 Final Review Questions

EL2310 Scientific Programming

Creating an object Instance variables

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Chapter 19 C++ Inheritance

Computer Programming

CPSC-112 Introduction to Programming

Computer Programming

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Java Classes & Primitive Types

Java Classes (Java: An Eventful Approach, Ch. 6),

Building Java Programs

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Transcription:

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