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

INTRO TO COMPUTER SCIENCE II
CONSTRUCTORS
CS162

Last time…
Finished Classes introduction Separation of files pt.2
Started to define the Course & Point classes

The Course class
Member variables are private
 Can’t initialize a Course object directly
Course c1 = {“CS162”,90,…};
Have to use each individual mutator function to initialize member variables
Annoying
class Course { private:
string name;
string roster[ENROLLMENT_CAP]; int current_enrollment;
public:
void set_name(string);
}; string get_name();
int main(){ Course c1;
c1.set_name(“CS 162”);
}

Constructors
Specially defined class functions
Automatically called when an object gets instantiated
Typically used to initialize member variables Appropriate default or user provided
Any steps needed for setup
If you don’t provide one, the compiler generates one
Implicit constructor
You should always provide one…  No values provided w/implicit

Constructors
Syntax rules
 Must have same name as class
 capitalization
 Not allowed to return anything
Not even void
Can have parameters
Can’t be called using the “.” operator like other class member functions
p1.set_location
p1.Point
class Point { private:
int x; int y;
public:
Point(); Point(int, int);
}; void set_location(int, int);
int main() { Point p1;
Point p2(8,4);
return 0;
}

Default Constructor
Constructor that takes no parameters Variables all have default values
Almost every class has one
 Example
P1 initializes to 0,0
Allocates memory for P1 object
Point::Point(){
x = 0;
y = 0; }
int main() { Point p1;
return 0; }

Constructors with Parameters
What if we want to initialize with specific values?
Two ways to define Variable assignments Initialization list
Point::Point(int a, int b){ x = a;
y = b; }
Point::Point(int a, int b):x(a),y(b){}
int main() { Point p1(8,4);
int main() { Point p1{8,4};
} return 0;
Can be called with either direct or } return 0; list initialization

Multiple Constructors
Two constructors now Default
 Accepts parameters
Can have as many as you want Function overloading
Each must have a unique signature
Point::Point(){
x = 0;
} y=0; Point::Point(int a, int b)
{ x=a;
y = b; }
int main() { Point p1;
Point p2(8,4);
return 0;
}

Reducing Constructors
Want to minimize the number contructors
Some can be redundant
Can set default values in a constructor
that accepts parameters
Still considered a default constructor
 But can accept a couple user provided values as well
Follows same rules as function defaults
Point::Point(int a=0, int b=0){ x = a;
y = b; }
int main() { Point p1;
Point p2(8,4);
Point p3(7);
return 0; }

Classes Containing Classes
Classes can contain other objects as member variables
When the outer class is constructed, the member variables’ default constructor is called
#include “point.h”
class Line { private:
Point start, end;
public:
Line();
}; Line(int, int, int, int);
class Point { private:
int x; int y;
public:
Point();
Point(int, int); };
Line::Line():start(1,1), end(2,2){}
Line::Line(int s_x, int s_y, int e_x, int e_y):start( s_x,s_y), end(e_x,e_y){}

Constructors
Don’t create objects
Compiler sets up memory for the object before constructor is
called
Determine who is allowed to create objects
Object can only be made if there is a matching constructor
Best practice to initialize all member variables on creation of object
 Constructors
Don’t use a constructor to re-initialize an object Compiler will create a temporary object and then discard it

Constructors & Functions
Constructors are allowed to have calls to non- constructor functions in the same class
What if you want to “re-initialize” an object to default values?
 Copy default constructor code into a non- constructor “init()” function
 Create “init()” and have constructor call it  Avoids duplicate code
 Be careful with dynamic memory allocation
class Point { private:
int x;
int y; public:
Point();
void init(); };
Point::Point(){
init(); }
void Point::init(){ x = 0;
y = 0; }
int main() { Point p1;
p1.move_left(3);
p1.init();
return 0;
}