C++ Short Course Part 1
• Classes
• Pointers
• Arrays
• Parameter passing
• Return values
C++ Short Course Part 1
• Classes
• Pointers
• Arrays
• Parameter passing
• Return values
Classes in C++:
Every variable has ______, ______, __________, ________
Primitive types:
int myFavoInt;
char rating = ‘E’;
double u = 37.;
User defined types:
sphere myFavoSphere;
______ is a group of ________ and _________
Structure of a class defn:
how do we implement sphere myFavoSphere; ?
class sphere{ sphere member function
//member declarations definitions.
};
Structure of a class defn (cont):
class sphere{ sphere representation:
public:
sphere functionality:
1.
2.
3.
int main(){
private:
};
}
Structure of a class defn (cont):
class sphere{ //constructor(s) (next page)
public: void sphere::setRadius(double newRad){
sphere();
sphere(double r); }
void setRadius(double newRad); double sphere::getDiameter() const {
double getDiameter() const;
…
}
…
private: Asides:
double theRadius; ______:
}; __:
Constructors (intro): When you declare a sphere, a sphere class constructor is invoked.
Points to remember abt ctors: …
1. //default constructor
sphere::sphere(){
}
2.
//default constructor, alternative
sphere::sphere()
3. {
//constructor with given radius
int main(){ sphere::sphere(double r){
} …
Class Definition… where are we?
Stepping back…
Ideas/concepts:
Class definitions
Class function implementation
Constructors
Clients
OOP: we now understand how C++ supports
Inheritance
Encapsulation (separation of interface from implementation)
1)
2)
Polymorphism 5
Our first class…
sphere.h main.cpp
class sphere{ #include “sphere.h”
};
int main(){
What surprises you about this code? sphere a;
}
1. Upon command > clang++ main.cpp does this code compile?
2. Upon command > ./a.out does it run?
Access control and encapsulation:
sphere.h main.cpp
class sphere{ #include “sphere.h”
#include <iostream>
double theRadius;
using namespace std;
};
What surprises you about this code?
int main(){
sphere a;
cout << a.theRadius << endl;
}
1. Upon command > clang++ main.cpp does this code compile?
2. Upon command > ./a.out does it run?
3. In c++ class members are, by default, “private”. Why would we want to hide
our representation of an object from a client?
4. How many collaborators are you allowed to have for PAs in this cpsc221?
C++ Short Course Part 1
• Classes
• Pointers
• Arrays
• Parameter passing
• Return values
Switching gears…
Variables and memory in C++
Stack memory
loc name value type
Pointers - Intro
Stack memory
int x; loc name value type
int * p;
How do we assign to p? a20 x 5 int
p =
a40 p int *
p =
_________ operator: &
_________ operator: *
Pointer variables and dynamic memory allocation:
Stack memory Heap memory
int * p; loc name type value loc name type value
a40 p int *
Youtube: pointer binky c++
Fun and games with pointers: (warm-up)
int * p, q; What type is q?______________
int *p;
int x;
p = &x;
*p = 6;
cout << x; What is output?______________
cout << p; What is output?______________
Write a statement whose output is the value of x, using variable p: ___________
int *p, *q;
p = new int;
q = p;
*q = 8;
cout << *p; What is output?______________
q = new int;
*q = 9;
p = NULL; Do you like this?_____________
delete q;
q = NULL; Do you like this?_____________
Memory leak:
Deleting a null pointer:
Dereferencing a null pointer: 18
Fun and games with pointers:
int * p, * q;
p = new int;
q = p;
delete p;
… // some random stuff
cout << *q; Do you like this?_____________
void fun() {
Stack vs. Heap memory: string * s = new string;
void fun() { *s = “hello?”;
string s = “hello!”; cout << *s << endl;
cout << s << endl; delete s;
} }
int main() { int main() {
fun(); fun();
return 0; return 0;
} }
System allocates space for s and Allocated memory must be deleted
takes care of freeing it when s goes programmatically.
out of scope.
Data must be accessed by a pointer.
Data can be accessed directly, rather
than via a pointer.
class face {
public:
void setName(string n);
Pointers and objects: string getName();
…
face a, b;
private:
… // init b
string name;
a = b;
PNG pic;
a.setName(“ann”);
boolean done;
b.getName();
};
face * c, * d;
… // init *d
c = d;
c->setName(“carlos”);
(*d).getName();
Practice--
int * p; int x; int * p;
p = x; *p = 37;
Do you like this?_____________ p = NULL;
What kind of error? Compiler Runtime *p = 73;
int * p, * q; Do you like this?_____________
p = new int; What kind of error? Compiler Runtime
q = p; int * p; int x;
delete p; Variable p can be given a target (pointee)
in two ways. Write an example of each.
… // some random stuff
cout << *q;
Use the letters S and H in a meaningful
way to tell where the pointee exists in
Do you like this?_____________ memory.
C++ Short Course Part 1
• Classes
• Pointers
• Arrays
• Parameter passing
• Return values
Arrays: static (stackic)
Stack memory
int x[5];
loc name type value
Arrays: dynamic (heap)
Stack memory Heap memory
int * x;
loc name value loc name value
int size = 3;
x = new int[size];
for(int i=0, i<size, i++)
x[i] = i + 3;
delete [] x;
A point to ponder: How is my garden implemented?
class garden{
Option 1:
public:
…
// all the public members
… Option 2:
private:
flower ** plot;
// other stuff
}; Option 3:
26
Option 4:
C++ Short Course Part 1
• Classes
• Pointers
• Arrays
• Parameter passing
• Return values
struct student {
string name;
PNG mug;
Parameter passing: bool printed; // print flag
};
int main() {
student a;
What happens when we
run code like this: print_student1(a); ?
}
bool print_student1(student s){
if (!s.printed)
cout << s.name << endl;
return true;
}
struct student {
string name;
PNG mug;
Parameter passing: bool printed; // print flag
};
bool print_student1(student s){
Function defn
if (!s.printed)
cout << s.name << endl;
return true;
}
student a;
Example of use
… // initialize a
a.printed = print_student1(a);
cout << a.printed << endl;
struct student {
string name;
PNG mug;
Parameter passing: bool printed; // print flag
};
void print_student2(student s){
Function defn
if (! s.printed)
cout << s.name << endl;
student * b;
Example of use
… // initialize b
print_student2(b);
cout << b.printed << endl;
struct student {
string name;
PNG mug;
Parameter passing: bool printed; // print flag
};
void print_student3(student s){
Function defn
if (! s.printed)
cout << s.name << endl;
}
Example of use
student c;
… // initialize c
print_student3(c);
cout << c.printed << endl;
C++ Short Course Part 1
• Classes
• Pointers
• Arrays
• Parameter passing
• Return values
struct student {
string name;
PNG mug;
bool printed; // print flag
};
Return values:
int main() {
student a;
What happens when we
run code like this: print_student1(a); ?
}
bool print_student1(student s){
if (!s.printed)
cout << s.name << endl;
return true;
}
Return by ___________ or ____________ or _____________ .
struct student {
string name;
PNG mug;
Returns: bool printed; // print flag
};
student * print_student5(student s){
Function defn
student w = s;
if (!w.printed){
cout << w.name << endl;
w.printed = true;
}
return &w;
}
Example of use
student c;
student * d;
… // initialize c
d = print_student5(c);
struct student {
string name;
PNG mug;
Returns: bool printed; // print flag
};
student & print_student5(student s){
Function defn
student w = s;
if (!w.printed){
cout << w.name << endl;
w.printed = true;
}
return w;
}
Example of use
student c,d;
… // initialize c
d = print_student5(c);
Lesson: don’t return 1) a pointer to a local variable, nor 2) a local variable by reference.