Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CSCI 104
Classes
Mark Redekopp
David Kempe
Sandra Batista
2
C Structs
• Needed a way to group values that are
related, but have different data types
• NOTE: struct has changed in C++! struct Person{
char name[20];
–C };
int age;
Coupling
• Coupling refers to how much components depend on each
other's implementation details (i.e. how much work it is to
remove one component and drop in a new implementation of
it)
– Placing a new battery in your car vs. a new engine
– Adding a USB device vs. a new video card to your laptop
• OO Design seeks to reduce coupling as much as possible by
– Creating well-defined interfaces to change (write) or access (read) the
state of an object
– Allow alternate implementations that may be more appropriate for
different cases
6
PARTS OF A CLASS
7
vector<int> dat(30);
return 0;
}
11
• s1
{
string s1;
string s2("abc");
– string::string()
// default constructor vector<int> dat(30);
• s2 return 0;
}
– string::string(const char* )
• dat
– vector<int>::vector<int>( int );
12
Member Functions
• Object member access uses
class Item
{ int val;
public:
dot (.) operator void foo();
void bar() const;
• Pointer-to-object member
};
void Item::foo()
{ val = 5; }
access uses arrow (->)
void Item::bar() const
operator { }
Exercises
• cpp/cs104/classes/const_members
• cpp/cs104/classes/const_members2
• cpp/cs104/classes/const_return
14
IMPORTANT DETAILS
16
•
this Pointer
How do member functions know which 0x7e0
poker.cpp
41 27 8 39 25 4 11 17
int main(int argc, char *argv[]) { d1
passed to shuffle()
top_index
Deck d1, d2; 1
d1 is implicitly
d1.shuffle();
deck.cpp
int r = rand() % (52-i); int temp = this->cards[r];
deck.cpp
with similar };
Exercises
• cpp/cs104/classes/this_scope
19
CONSTRUCTOR INITIALIZATION
LISTS
20
Exercises
• cpp/cs104/classes/constructor_init
21
struct Student
{
std::string name; string name
int id;
int id
std::vector<double> scores;
// say I want 10 test scores per student scores
};
int main()
{
Student s1;
}
22
Composite Objects
int main()
{ Student s1;
//...
}
23
Composite Objects
• You cannot call constructors on data members once the
constructor has started (i.e. passed the open curly '{' )
– So what can we do??? Use initialization lists!
#include <string>
#include <vector>
struct Student
{ std::string name;
int id;
std::vector<double> scores;
// say I want 10 test scores per student
string name
Student() /* mem allocated here */
{ // Can I do this to init. members? int id
This would be
name("Tommy Trojan"); scores
"constructing"
id = 12313;
name twice. It's
scores(10);
too late to do it in
}
the {…}
};
int main()
{ Student s1;
//...
}
24
You can't call member You would have to call the member
constructors in the {…} constructors in the initialization list context
Summary
Student::Student() Student::Student() :
{ name(), id(), scores()
name = "Tommy Trojan"; // calls to default constructors
id = 12313 {
scores.resize(10); name = "Tommy Trojan";
} id = 12313
scores.resize(10);
}
You can still assign data But any member not in the initialization list will
members in the {…} have its default constructor invoked before the
{…}
• You can still assign values in the constructor but realize that the
default constructors will have been called already
• So generally if you know what value you want to assign a data
member it's good practice to do it in the initialization list
Student::Student() :
name("Tommy"), id(12313), scores(10)
{ }
Exercises
• cpp/cs104/classes/constructor_init2
28
Multiple Inclusion
class string{
• Often separate files may ... };
#endif ...
main.cpp after preprocessing
31
CONDITIONAL COMPILATION
32
Conditional Compilation
• Often used to compile int main()
additional DEBUG code {
int x, sum=0, data[10];
– Place code that is only needed for ...
debugging and that you would not want to for(int i=0; i < 10; i++){
execute in a release version sum += data[i];
#ifdef DEBUG
• Place code in a cout << "Current sum is ";
cout << sum << endl;
#ifdef NAME...#endif bracket #endif
}
• Compiler will only compile if a
cout << "Total sum is ";
#define NAME is found cout << sum << endl;