cppShortCourse1-ann
cppShortCourse1-ann
• 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; ?
};
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):
}; __:
Constructors (intro): When you declare a sphere, a sphere class constructor is invoked.
}
2.
//default constructor, alternative
sphere::sphere()
3. {
} …
Class Definition… where are we?
Stepping back…
Ideas/concepts:
Class definitions
Class function implementation
Constructors
Clients
2)
Polymorphism 5
Our first class…
sphere.h main.cpp
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
p =
a40 p int *
p =
_________ operator: *
Pointer variables and dynamic memory allocation:
Stack memory Heap memory
a40 p int *
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:
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;
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];
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); ?
}
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
};
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
};
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); ?
}
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 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.