CS计算机代考程序代写 scheme compiler INTRO TO COMPUTER SCIENCE II

INTRO TO COMPUTER SCIENCE II
OBJECT ORIENTED PROGRAMMING
CS162

Object-Oriented Programming
What is an object?
Piece of memory for holding values
Traditional Programming
 Define data – using an object (variables, structs)
 Work with data – using statements or functions
 Up to you to connect properties (objects) with behavior (functions)
//pseudocode
object you;
object shark;
function eat(you, shark)

Object-Oriented Programming
Create objects that combine both properties and behaviors Example: Student
Properties – name, age, major Behavior – talking, walking, sleeping
Why use OOP?
Easier to read/write, because it makes the subject of behavior clearer Enables us to use inheritance, polymorphism, encapsulation, and abstraction
//pseudocode
object you;
object shark;
shark.eat(you)

Object-Oriented Programming
 Classes
User defined datatype, basic building block Similar to structs
Has both member variables and member functions
class Book {
string title;
string author;
int pages;
void print_info(){ } //print stuff
};
Why use classes? Adds functionality Same as OOP reasons
ifstream fin;
fin.is_open();
Book b;
b.print_info();

Object-Oriented Programming
Classes vs. Structs
 Functionality vs. No functionality  Private vs. public by default
struct book {
string title;
string author;
int pages; };
class Book {
public:
string title;
string author;
int pages;
void print_info();
};
Access specifiers
 Public – members can be accessed outside of the class
 Private – members can only be accessed by other members of the class

Vocabulary so far…
Struct – an object without any member functions; collection of data items of diverse types
Class – an object with both member variables and member functions
Object – an instance of the class
Member variable – variable that belongs to a particular class/struct Member Function – function that belongs to a particular class

Classes
Book class
Contains a title, author,
number of pages
Member functions for setting book info, getting book info
class Book { public:
string title; string author; int num_pages;
void set_info(string, string, int);
string get_title();
};
void Book::set_info(string t, string a, int p) { title = t;
author = a;
num_pages = p;
}
string Book::get_title() {
return title;
}

Classes
Point class
 Contains an X value and a Y value
 Can create member functions to move the point, display the value, or perform other manipulations
class Point { public:
int x; int y;
void move_left(int); };
void Point::move_left(int delta) {
x = x – delta; }
int main() {
Point p1;
p1.x = 8;
p1.y = 4;
return 0;
}

Encapsulation
Hide the details of your class from others
 Why?
Makes class easier to maintain Helps avoid broken code
Consider the Point class
 What if we changed int x; to int x_position;?
 Bad news for anybody using our class previously
class Point { public:
int x_position; int y_position;
void move_left(int); };
void Point::move_left(int delta) { } x_position = x_position – delta;
int main() {
Point p1;
p1.x = 8;
p1.y = 4;
return 0; }

Implementing Encapsulation
Accessor functions
Function that retrieves values
Difference between implementation (your code) and interface “getters”
get_x(), get_y();
Mutator functions Function that sets values “setters”
Use a consistent naming scheme

How do we enforce our plan?
C++ includes the concept of access specifiers
 Public – these functions and variables are available to any code that includes the header file
 Private – can only be accessed or modified by code within the same class
class Point { private:
int x; int y;
public:
void move_left(int);
void set_location(int, int); int get_x();
int get_y();
};
int main() {
Point p1, p2;
p1.set_location(8, 4);
p2.set_location(13, 10);
return 0;
}

Why are accessors and mutators critical?
In combination with access specifiers, accessors and mutators allow us to control access
Especially useful when you want to have “read-only” member variables
Users can read with the getter, but they can’t change it without a setter

How secure are access specifiers?
This is not meant to prevent people from looking at your source code
A programmer could still open your .cpp file and look at the names of “private” variables
The concept of public and private members is enforced by the compiler
You will receive a compile-time error if you try to access unauthorized variables or functions