Professional Documents
Culture Documents
C++ expands dynamic allocation to objects Similar to struct arrays, class can be used to
–allow malloc()/free() and new/delete create object arrays
– malloc() does not call the constructor –format: 〈CName〉 〈pObj〉[iSize];
when an object is created –use public member: 〈pObj〉[iIdx].member;
–Use new/delete for objects Example: lec4-1.cpp (part 1)
Heap objects CScore StuAry[3];
//CScore[0]=(“Adam”,{43,62,85});
–Format: //CScore[0]=(“Bill”,{72,82,93});
〈CName〉 * 〈pObj〉 = new 〈CName〉; //CScore[0]=(“Nick”,{31,53,76})};
delete 〈pObj〉; double sum = 0;
–Ex: for (int i=0; i<3; i++) {
CScore * pStu = new CScore; sum += StuAry[i].computeAverage();
pStu->computeAverage(); }
delete pStu; cout << sum/3.0 << endl;
Leture 04 UEE1303(1070) 3 Leture 04 UEE1303(1070) 4
Pointer to Member Function (1/2) Pointer to Member Function (2/2)
Similar to the pointer to member function, the How C++ guarantee that the data member is
pointer to data member has the format correctly referenced via member function?
〈datatype〉 (〈CName〉::* 〈pName〉); Example:
〈pName〉 = &〈CName〉::〈data_member〉;
class Box {
Ex: see more in lec4-1.cpp(part 3) public:
class X { Box(int h=6, int w=8, int l=10):
public: hgt(h), wid(w), len(l) {}
int a; int vol() { return hgt*wid*len; }
void f(int b) { cout << b << endl;} private:
}; int hgt, wid, len;
int X::* pa = &X::a; };
X obj1; Box a(2,4,6), b(3,5,7);
obj1.*pa = 10;
cout << “value a = ” << obj1.*pa; – a.vol() and b.vol() use the same function
Leture 04 UEE1303(1070) 7 Leture 04 UEE1303(1070) 8
this Pointer (2/3) this Pointer (3/3)
Each member function contains a special Explicitly use this pointer if needed
pointer named this //in Box class declaration, version 1
–points to the address of the called object int vol() {
(this->hgt)*(this->wid)*(this->len); return ((*this).hgt)*((*this).wid)
*((*this).len); }
–If call a.vol(), equivalently,
(a->hgt)*(a->wid)*(a->len); –pair of parentheses cannot be omitted
– this pointer is used implicitly but works like
because dot (.) operator precedes
//in Box class declaration, version 2 dereference (*) operator
int vol(Box * this) { –If *this.hgt is interpreted as *(this.hgt)
return (this->hgt)*(this->wid) but this is a pointer and this.hgt cannot
*(this->len); }
and call by be parsed correctly
a.vol(&a); Compilation error! use this->hgt
Leture 04 UEE1303(1070) 9 Leture 04 UEE1303(1070) 10
A this pointer stores the address of an object C++ has many choices of members
used to call a non-static member function – static members: members that all objects
–typically hidden from programmer of that class share
–handled by the compiler
– const members: members that require
–address of that object is passed to the
initialization and cannot be updated
function and stored in this
–access the data stored in the object by – mutable members : members that need no
dereferencing this, i.e. this->[member] protection from changing any time but are
–use when returning the object itself or when encapsulated in class
parameters have the same name as private – explicit members: members that avoid
data members ambiguity from calling constructor implicitly
be aware of this for advanced OOP –and more (reference members)
Leture 04 UEE1303(1070) 13 Leture 04 UEE1303(1070) 14
Class provides data encapsulation and hiding A static data member is defined by
–but results in difficulties data sharing and –use keyword static to declare a data
external visit member in class
–can be resolved by global variables –allocate memory and initialize outside class
A better solution static members –not limited by the access modifier
–is specific to one class Example
–has a scope shared by the objects of the class X {
same class private:
A static member can be accessed //declare a static data variable
static int count;
(1) with class methods or };
(2) outside class methods //mtd(1): initialize the static member
〈type〉 〈class〉::〈static_data〉 = 〈value〉; int X::count = 0;
Leture 04 UEE1303(1070) 15 Leture 04 UEE1303(1070) 16
Access with Class Methods Use Static Members (1/3)
Use static data members from class methods Design a CScore class with data members: id
class CNum { number (id), name (name), three subject scores
public:
CNum(int a) { x = a; y += x; } (subj[3]) , and total sum of each subject
static void fun(CNum m) { scores (sum[3]). Write functions to compute the
cout << m.x << “vs.” << y << endl; } sums of each subjects.
private:
int x; //lec4-3.cpp (part 1)
class CScore //data members in CScore
static int y; {
}; private:
int CNum::y = 0; char id[6];
//in main() char name[20];
CNum O1(4), O2(7); int subj[3];
CNum::fun(O1); static int sum[3];
CNum::fun(O2); What to display on screen? }
Leture 04 UEE1303(1070) 17 Leture 04 UEE1303(1070) 18
Constants almost never make sense at the const can used with & modifier need to be
object level often used with static modifier initialized in constructors
class CScore { //lec4-3.cpp (part 2) class CScore { //lec4-3.cpp (part 2)
protected: public:
//declare a static constant //declare a constant reference
static const int Max; const int & sc;
}; };
const int CScore::Max = 100; //outside class
//modified constructor in CScore
//modified constructor in CScore CScore::CScore(char *uid, char *uname,
CScore::CScore(char *uid, char *uname, int *uscore) : sc(subj[0]) {
int *uscore) { subj[0] = (uscore[0]>Max)?Max:uscore[0];
subj[0] = (uscore[0]>Max)?Max:uscore[0]; subj[1] = (uscore[1]>Max)?Max:uscore[1];
subj[1] = (uscore[1]>Max)?Max:uscore[1]; subj[2] = (uscore[2]>Max)?Max:uscore[2];
subj[2] = (uscore[2]>Max)?Max:uscore[2]; ...}
...}
Leture 04 UEE1303(1070) 21 Leture 04 UEE1303(1070) 22
Only constant methods can call a const mutable means volatile constant
object or a reference to const object –a constant function cannot modify any data
class CScore { member which may not need protection
public: //add more const methods mutable makes data member always
char* getName() const { return name; } changeable, even in a const method
void setName(const char* uname) {
strcpy(name, uname);} //not const –cannot modify methods but regular variables
}; –can modify const and static, not reference
//CScore S1(“99123”, “Tom”, {70, 80, 90}); class CScore {//lec4-3.cpp (part 3)
cout << S1.getName() << endl; //OK! public:
S1.setName(“Tom Mitchell”); //OK! mutable int sCount = 0; };
const CScore & S3 = S1;
cout << S3.getName() << endl; //OK! void CScore::showName() const {
S3.setName(“Bob”); //compilation error! cout<<sCount++<<“:”<<name<<endl; }
Leture 04 UEE1303(1070) 23 Leture 04 UEE1303(1070) 24
More on mutable Members explicit Members
Nested class : a class CTwo is declared within class Node { ... }; //outer class
the scope of another class COne class Graph {
public:
–used when the inner class is meaningful //Graph::Node hides ::Node in Graph’s {}
inside the outer class class Node { ... }; //inner class
//resolves to nested Graph::Node
–if CTwo is public, use with COne:: Node *grpah;
–format: class COne { //outer class };
... //Graph::Node is not visible globally
class CTwo { //inner class Node *pnode;
class Chain {
... public:
}; //Chain::Node hides ::Node in Chain’s {}
}; class Node { ... }; //inner class
–Nested class cannot access any private //resolves to nested Chain::Node
Node *chain;
member of the outer class by default };
Leture 04 UEE1303(1070) 27 Leture 04 UEE1303(1070) 28
More on Nested Classes Defined Outside Outer Class
A nested class CTwo in COne can have the A nested class can also be defined outside
same kind of members as a nonnested class the enclosing class
–Example: –need to take care of qualifier
class Chain { //outer class –only pointers and references to the inner
public: class can be declared
class Item { //all members are private class COne { //enclosing class
friend class Chain; class CTwo; //nested class
Item(int val = 0); //constructor CTwo *pobj; //CTwo obj is wrong!!!
Item *next; //point to its own class
int val; //...
}; };
//... class COne::CTwo {
private: CTwo(int val=0);
Item *chain; CTwo *next;
Item *at_end; int value;
}; };
Leture 04 UEE1303(1070) 29 Leture 04 UEE1303(1070) 30
A nested class can be first declared and then Hiding data inside a class and letting only
later defined in the outer class class member functions have direct access to
private data is a very important OOP concept
class Chain { //outer class But C++ also provides another of function to
private:
class Item; //declare Chain::Item access members in class friend functions
class Ref { – friend functions are not member functions
Item *pIt; //has type Chain::Item*
}; but can still access class private members
class Item { //later define Chain::Item –defined outside of class scope
Ref *pref; //has type Chain::Ref* Reasons to have friend functions:
};
//... –to access private members of two or more
}; different classes
–for I/O or operator (in Lecture 6) functions
Leture 04 UEE1303(1070) 31 Leture 04 UEE1303(1070) 32
Properties of friend Functions Example of friend Functions
//lec4-4.cpp
Properties: class CPoint {
–placed inside the class definition and int x, y;
preceded by the friend keyword friend CPoint offset(CPoint &, int);
public:
–defined outside the class as a normal, non- CPoint() { x=0; y=0; }
member function CPoint(int a, int b) { x=a; y=b; }
void Print() { cout << x << “ ”
–called like a normal non-member function. << y << endl; }
If a function is a friend of two or more };
CPoint offset(CPoint &pt, int diff) {
different classes, each class must contain the pt.x += diff; pt.y += diff;
friend function prototype within its body return pt;
}
A friend function cannot be inherited (covered
//in main()
in Lecture #7) but can be a member of one CPoint p1( 3, 5 ); p1.Print();
class offset(p1, 4); p1.Print();
Leture 04 UEE1303(1070) 33 Leture 04 UEE1303(1070) 34