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

INTRO TO COMPUTER SCIENCE II
CLASSES
CS162

Tips & Tricks
If you’ve been staring at your code for a long time, take a short break (check out the Pomodoro method)
If you’re trying to think through a problem  rubber duck it
 Draw it out
Save and test frequently, with comments
Don’t copy…
Check that all files needed are actually in your tarball before AND after you submit, and that it compiles on the ENGR servers

Last time…
Classes vs. Structs
 Encapsulation Point class example

Last time…
Classes vs. Structs
 Classes have functionality and are private by default  Structs have only variables and are public by default
 Encapsulation
 Access modifiers – public, private
 Accessors/mutators – getters and setters
Point class example

File Separation (this time with classes)
Classes are typically written with their own header (.h) and implementation (.cpp) files
Point.h – contains the class definition and member function prototypes and variables
Point.cpp – contains the member function definitions prog.cpp – driver file still the same, includes the .h and has main

File Separation (pt. 2)
Point.h #ifndef POINT_H
#define POINT_H
#include
using namespace std;
class Point { private:
int x; int y;
public:
void move_left(int);
void set_location(int, int); int get_x();
int get_y();
};
#define
Point.cpp
using namespace std;
void Point::move_left(int dx) {
x -= dx; }
void Point::set_location(int new_x, int new_y) { x = new_x;
y = new_y; }
int Point::get_x() {
return x; }
int Point::get_y() {
return y; }
#include
#include “point.h”

Understanding the Concept of an Object
By default, each object has its own personal copy of each member variable
 This is a crucial observation!
Consider the Point class
 If you create Points p1 and p2, they are independent
 Modifying the X location of p1 will not change the X location of p2

Point Objects
int main() {
// create an instance of the Point class
// we would now call p1 and p2 “point objects” Point p1, p2;
p1.set_location(8, 4);
p2.set_location(13, 10);
// this would cause a compile-time error (since x and y are private member variables) //p1.x = 3;
cout << "Point p1 info: x = " << p1.get_x() << " y = " << p1.get_y() << endl; cout << "Point p2 info: x = " << p2.get_x() << " y = " << p2.get_y() << endl; // move point p1 to the left p1.move_left(3); cout << "Point p1 info: x = " << p1.get_x() << " y = " << p1.get_y() << endl; cout << "Point p2 info: x = " << p2.get_x() << " y = " << p2.get_y() << endl; return 0; } Implementing a Class – In-class Activity Let’s use what we’ve learned so far to create a Course class. Work in groups (i.e. breakout rooms).  Create header and implementation files  Basic properties include: Course name, Instructor, Roster, Etc. Questions to think about: What type would each property be? What behaviors would a Course class have?