Try and Buy -- Costco Tasting Study Approach
-- Try and Error: managing the student profile with IT
It drives me crazy
The IT management needs to declare some
attributes to characterize the student.
// Scattered flowers
string name;
char
gender;
string major;
void get_data(string& n, char& g, string& m)
{
……
}
void display(string n, char g, string m)
{
cout << “Name:
” << n << endl;
cout << “Sex: ” << g << endl;
cout << “Major:
” << m << endl;
It is improved with ease
Structure in C/C++ allows the programmer to Class in C/C++ is adapted from structure
group many attributes as a whole
allows the programmer to group both data and
function as a whole
// cstudent.h
// ONE object – a bunch of flowers
struct TStudent {
string
name;
char
gender;
string
major;
};
void get_data(TStudent& st) // by reference
{
…
}
void display(TStudent st) // by value
{
cout << “Name: ” << st.name << endl;
cout << “Sex: ” << st.gener << endl;
cout << “Major: ” << st.major << endl;
}
class CStudent {
string
name;
char
gender;
string
major;
void get_data();
void display();
};
//cstudent.cpp
void CStudent::get_data()
{ … }
void CStudent::display()
{
cout << “Name: ” << name << endl;
cout << “Sex: ” << gener << endl;
cout << “Major: ” << major << endl;
}
A1:
A2:
easy to expand
easier to manage more students
TStudent CS220_Roster[14];
P3:
P4:
Consideration of pass-by-val/ref
Dot-operator for accessing members
st.name / st.gender / st.major
A3:
no need (pass-by-val/ref)
A4:
No need (dot-operator)
#include “cstudent.h”
int main()
{
CStudent Coba;
// single
Coba.get_data();
Coba.display();
}
P1:
P2:
P3:
Hard to expand with more attributes
Harder to manage from one to many students
Consideration of pass-by-val/ref
It is amazing creatively
CStudent CS220_st[14]; // array
for(int i=0; i<14; i++)
{
CS220_st[i].get_data();
CS220_st[i].display();
}
}