1 CPP
1 CPP
Programming
Yung Yi
1
Ack
• Big Thanks
ü These slides are largely borrowed from Prof. Takgon
Kim’s Slides
2
Goals of This Lecture
• Overview of C++ language
ü At a glance, C++ = C + Class
3
Objected Oriented
Programming
4
The C++ Programming Model
CoolProgram.cpp
a.out
Library Functions
Compiler
User
5
A Simple C++ Program
• Two integer inputs x and y
• Output their sum
#include <cstdlib>
#include <iostream>
/* This program inputs two numbers x and y and outputs their sum */
int main( ) {
int x, y;
std::cout << “please enter two numbers: ”
std::cin >> x >> y; // input x and y
int sum = x + y; // compute their sum
std::cout << “Their sum is ” << sum << std::endl;
return EXIT_SUCCESS // terminate successfully
}
6
Abstraction and Abstract Data Type
• Abstraction: depends on what to focus
ü Procedure abstraction: focuses on operations
ü Data abstraction: data + operations as one
ü Object abstraction: data abstraction + reusable sub types (class)
• Abstract data type (ADT)
ü Definition of a set of data + associated operations
• Implementation of ADT
ü Data à data structure
w Stack, Queue, Tree etc.
ü Operations à manipulation of data structure
w Stack: push, pop etc.
ü Error conditions associated with operations
7
Example of ADT
• Example: ADT modeling a simple stock trading system
ü The data stored are buy/sell orders
ü The operations supported are
w order buy(stock, shares, price)
w order sell(stock, shares, price)
w void cancel(order)
ü Error conditions:
w Buy/sell a nonexistent stock
w Cancel a nonexistent order
8
C & C++ in Abstraction View
9
Procedural-Oriented VS. Object-Oriented
Procedural-Oriented Program Object-Oriented Program
main Object-1
function-1 data-1 Object-2
data +
(procedure) functions data-2
data +
functions
function-2
(procedure)
data
Object-n
Object-3 data-4
function-3 data-3 +
+ functions
(procedure) functions
data
data is open to all functions. Each data is hidden and associated with an object.
10
Example: PO VS. OO
CAR
Object-oriented View
Procedure-oriented View
of car operation
of car operation
steering accelera
switch on the ignition wheel
brakes
tor
accelerate
tires engine
steer
brake
Car = a sequence of functions (procedures) Car = interaction between components (objects)
11
What is Object ?
• Class (« Type in C )
ü Defines the abstract characteristics of a thing (object)
w attributes (data) + behaviors (operations = methods)
• Object (« Variable in C )
ü A pattern (exemplar) of a class
• Instance
ü The actual object created at runtime
ü State: the set of values of the attributes of a particular object
• Methods Car
ü functions Class Attributes
: color, capacity, max. speed, …
Methods
Ferrari Hyundai : accelerate, brake, steer left,
Object steer right, …
(Instance at runtime)
12
C++ Classes
• Similar to structure in C
Class in C++ Structure in C
class class_name {
struct tag_name {
public:
type1 member1;
// member variables
int a, b, c; type2 member2;
… …
// member methods (functions) typeN memberN;
void print(void); };
…
};
13
Class Declaration
class_name instance_name1, instance_name2;
C.f. struct tag_name struct_variable, … ;
class_name
instantiation instance_name2
variables
variables
methods
instance_name1 methods
variables
methods
14
C Style Design (Procedural) (1/2)
Bank
Client
data operations
data
name
operations Client
name
telephone no. telephone no. withdrawal
withdrawal
account no. account no.
deposit
password
password deposit
balance
…
…
balance
…
15
C Style Design (Procedural) (2/2)
Array of structures
Structure Functions
Bank
struct client {
char name[MAX];
Client char tel[MAX];
name
char account[MAX];
telephone no. withdrawal
char password[MAX];
account no. int balance;
deposit
password };
balance
…
… struct client clients[MAX_NO];
…
void withdrawal (client &cli, int money);
void deposit (client &cli, int money);
references
16
C++ Style Design (Object-Oriented) (1/2)
Bank
Client
data
name
operations Client
name withdrawal
telephone no. telephone no. deposit
withdrawal
account no. account no. …
password …
password deposit
balance
balance …
17
C++ Style Design (Object-Oriented) (2/2)
Class
Bank class client {
char name[MAX];
char tel[MAX];
Client char account[MAX];
name withdrawal char password[MAX];
telephone no. deposit int balance;
void withdrawal (int money);
account no. …
password … void deposit (int money);
};
balance member variables
are not required
…
client clients[MAX_NO];
18
Example: Class
#include<iostream> int main( ) {
#define MAX 10 instantiation record myrecord;
using namespace std; myrecord.name = “KIM JH”;
referencing
public member myrecord.course1 = 100;
class record{ variables myrecord.course2 = 90;
public: Access specifier int sum = myrecord.course1 +
char name[MAX]; myrecord.course2;
member variables
int course1, course2; myrecord.avg = ((double) sum) / 2;
double avg; myrecord.print( );
member function call
void print(void) { return 0;
cout << name << endl; }
cout << “course1 = ” << course1
member function
<< “, course2 = ” << course2 << endl;
cout << “avg = ” << avg << endl; result>
} KIM JH
}; course1 = 100, course2 = 90
avg = 95
19
Definition of Member Functions
whole code in same file
class record{ ex) “record.cpp” class record{
public: public:
char name[MAX]; char name[MAX];
int course1, course2; int course1, course2;
double avg; double avg;
void print(void) { void print(void); declaration
“record.h”
definition :
cout << name << endl; }; always after declaration
cout << “course1 = ” << course1
“record.cpp”
<< “, course2 = ” << course2 void record::print(void) {
<< endl; cout << name << endl;
cout << “avg = ” << avg << endl; cout << “course1 = ” << course1
} << “, course2 = ” << course2 << endl;
}; cout << “avg = ” << avg << endl;
declaration & definition }
20
Member Variables & Functions
#include<iostream> int main( ) {
#define MAX 10 record myrecord;
using namespace std; myrecord.name = “KIM JH”;
always must reference myrecord.course1 = 100;
class record{ member variables myrecord.course2 = 90;
public: with instance name int sum = myrecord.course1 +
char name[MAX]; myrecord.course2;
int course1, course2; myrecord.avg = ((double) sum) / 2;
double avg; myrecord.print( );
void print(void) { return 0;
cout << name << endl; }
cout << “course1 = ” << course1 can reference member variables
without class name
<< “, course2 = ” << course2
inside member functions
<< endl;
cout << “avg = ” << avg << endl;
}
member function
};
21
Encapsulation
• Encapsulation conceals the functional details defined in
a class from external world (clients).
ü Information hiding
w By limiting access to member variables/functions from outside
ü Operation through interface
w Allows access to member variables through interface
ü Separation of interface from implementation
w Similar to Stack data type and implementation (Lecture 11)
no necessity for
knowing inside
encapsulation
Class in C++
class class_name {
public:
int a, b, c;
Interfaces : open outside
…
void print(void);
Access specifier
…
private:
… Encapsulation: hide inside
protected:
…
};
23
Basic Features
(Mostly same as C)
24
C++ Data Types
25
Fundamental Types
• Basic data types
ü bool Boolean value, either true or false
ü char Character
ü short Short integer
ü int Integer
ü long Long integer
ü float Single-precision floating-point number
ü double Double-precision floating-point number
ü enum User-defined type, a set of discrete values
ü void The absence of any type information
26
Declaration of a Variable
• We can provide a definition, or initial value
• Without definition, initial value is zero
• Variable names may consist of any combination of
letters, digits, or the underscore (_) character, but the
first character cannot be digit
• ex)
short n;
int octalNumber = 0400;
char newline_character = ‘\n’;
long BIGnumber = 314159265L;
short _aSTRANGE__1234_variABlE_NaMe;
27
Characters: char
• Typically 8-bit
• Literal
ü A constant value appearing in a program
ü Enclosed in single quotes
ü A backslash (\) is used to specify a number of special
character literals
28
Integers: short, int, long
• Short int, (plain) int, long int
• Decimal numbers
ü ex) 0, 25, 98765, -3
• Suffix “l” or “L” indicate a long integer
ü ex) 123456789L
• Prefix “0” indicates octal constants
ü ex) 0400 (256)
• Prefix “0x” indicates hexadecimal constants
ü ex) 0x1c (28)
29
Floating Point: float, double
• Floating point literals
ü ex) 3.14159, -1234.567, 3.14E5, 1.28e-3
• Default is double type
• Suffix “f” or “F” indicate float
ü ex) 2.0f, 1.234e-3F
30
Enumerations: enum
• A user-defined type that can hold any of a set of
discrete values
• Once defined, enumerations behave much like an
integer type
• Each element of an enumeration is associated with an
integer value
• ex)
32
Pointers
• ex) char ch = ‘Q’;
char* p = &ch; // p holds the address of ch
cout << *p; // outputs the character ‘Q’
ch = ‘Z’; // ch now holds ‘Z’
cout << *p; // outputs the character ‘Z’
33
Arrays
• A collection of elements of the same type
• Index references an element of the array
• Index is a number from 0 to N-1
• ex)
34
Arrays
• Two-dimensional array
ü An “array of arrays”
ü ex) int A[15][30]
• Initializing
ü ex)
int a[4] = {10, 11, 12, 13}; // declares and initializes a[4]
bool b[2] = {false, true}; // declares and initialize b[2]
char c[] = {‘c’, ‘a’, ‘t’}; // declares and initialize c[3]
// compiler figures the size of c[]
35
Pointers and Arrays
• The name of an array can be used as a pointer to the
array’s initial element and vice versa
• ex)
36
C-Style Structure
• Storing an aggregation of elements which can have
different types
• These elements called “member” or “field”, is referred
to by a given name
• ex)
struct Passenger {
string name; // possible value: “John Smith”
MealType mealPref; // possible value: VEGETARIAN
bool isFreqFlyer; // possible value: true
string freqFlyerNo; // possible value: “293145”
};
37
C-Style Structure
• This defines a new type called Passenger
• Declaration and initialization
ü ex) Passanger pass = { “John Smith”, VEGETARIAN, true, “293145” }
39
Constants
• Adding the keyword const to a declaration
• The value of the associated object cannot be changed
• ex) const double PI = 3.14159265;
const int CUT_OFF[] = {90, 80, 70, 60};
const int N_DAYS = 7;
const int N_HOURS = 24*N_DAYS; // using a constant expression
int counter[N_HOURS]; // constant used for array size
40
Typedef
• Define a new type name with keyword typedef
• ex)
41
Dynamic Memory Allocation
42
Dynamic Memory and ‘new’ Operator
• Create objects dynamically in the ‘free store’
• The operator ‘new’ dynamically allocates the memory
from the free store and returns a pointer to this object
• Accessing members
ü pointer_name->member
ü (*pointer_name).member
ü Same as how to access a member in C Struture
Passenger *p;
//...
p = new Passenger; // p points to the new Passenger
p->name = “Pocahontas”; // set the structure members
p->mealPref = REGULAR;
p->isFreqFlyer = false;
p->freqFlyerNo = “NONE”;
//...
delete p; // destroy the object p points to
44
Example: Operators for Dynamic Allocation
C C++
Functions Operators
void * malloc ( size_t size ) new data_type returns a pointer
void * calloc (size_t nmemb, size_t size ) new data_type[size] addressing the 1st
void free(void *ptr); delete scalar_variable; element of the array
delete [] array_variable;
Ex) To allocate a char
C C++
char *cptr; char *cptr = new char;
cptr = (char *) malloc(sizeof(char)); …
… delete cptr;
free(cptr);
• You can use your own method, but you can also use ’vector’
class in STL library
46
Memory Leaks
• C++ does not provide automatic garbage collection
47
Strings in C++
48
Strings
• C-style strings
ü A fixed-length array of characters that ends with the null
character
ü This representation alone does not provide many string
operations (concatenation, comparison,...)
• STL strings
ü C++ provides a string type as part of its “Standard Template
Library” (STL)
ü Should include the header file “<string>”
C C++
array of char types string class
library functions member functions of string class
relatively difficult, but many sources easy
50
STL Strings
• ex)
#include <string>
using std::string;
//...
string s = “to be”;
string t = “not ” + s; // t = “not to be”
string u = s + “ or ” + t; // u = “to be or not to be”
if (s > t) // true: “to be” > “not to be”
cout << u; // outputs “to be or not to be”
51
STL Strings
• Appending one string to another using += operator
• Indexed like arrays
• The number of characters in a string s is given by
s.size()
• Conversed to C-style string by s.c_str() which returns a
pointer to a C-style string
52
STL Strings
• ex)
s = “John”; // s = “John”
int i = s.size(); // i = 4
char c = s[3]; // c = ‘n’
s += “ Smith”; // s = “John Smith”
char *p = s.c_str(); // p is a C-style string
53
C Style String to C++
Result>
#include<iostream>
cstyle = KKIST
#include<string>
cppstyle = KAIST
using namespace std;
main() {
char cstyle[] = "KKIST";
string cppstyle;
cppstyle = cstyle;
cppstyle[1] = 'A';
54
C++ Style String to C (1/2)
#include<iostream>
#include<string>
using namespace std;
main() {
string cppstyle = "KAIST";
const char *cstyle;
55
C++ Style String to C (2/2)
#include<iostream>
#include<string>
using namespace std;
main() {
string cppstyle = "KKIST";
char* cstyle = new char [ cppstyle.size() + 1];
cstyle[1] = 'A';
56
Scope, Namespace,
Casting, Control Flow
57
Local and Global Variables
• Block
ü Enclosed statements in {...} define a block
ü Can be nested within other block
• Local variables are declared within a block and are only
accessible from within the block
• Global variables are declared outside of any block and
are accessible from everywhere
• Local variable hides any global variables of the same
name
58
Local and Global Variables
• ex)
int main () {
const int cat = 2; // this cat is local to main
cout << cat; // outputs 2 (local cat)
return EXIT_SUCCESS;
}
int dog = cat; // dog = 1 (from the global cat)
59
Scope Resolution Operator (::)
#include <iostream>
using namespace std; result>
local x = 1
int x; global x = 2
int main()
{
int x; local x hides global x
x = 1;
::x = 2; assign to global x
return 0;
}
60
Namespaces: Motivation
• Two companies A and B are working together to build a game software
“YungYung”
• Compile? Failure
• Solution
ü A: struct Atree {}; B: int BTree; à dirty, time consuming, inconvenient
namespace myglobals {
int cat;
string dog = “bow wow”;
}
myglobals::cat = 1;
62
The Using Statement
• Using statement makes some or all of the names from
the namespace accessible, without explicitly providing
the specifier
• ex)
63
Example : Namespace
#include <iostream>
namespace IntSpace{
int data;
void add(int n){ data += n; }
void print(){ std::cout << data << std::endl; } same variable name is allowed in
} different namespaces
namespace DoubleSpace{
double data;
void add(double n){ data += n; }
void print(){ std::cout << data << std::endl; }
}
int main()
{
IntSpace::data = 3;
DoubleSpace::data = 2.5;
IntSpace::add(2);
DoubleSpace::add(3.2);
IntSpace::print(); result>
DoubleSpace::print(); 5
return 0; 5.7
}
64
Traditional C-Style Casting
int cat = 14;
double dog = (double) cat; // traditional C-style cast
double pig = double(cat);// C++ functional cast
int i1 = 18;
int i2 = 16;
double dv1 = i1 / i2; // dv1 = 1.0
double dv2 = double(i1) / double(i2); // dv2 = 1.125
double dv3 = double( i1 / i2); // dv3 = 1.0
65
Static Casting (to give “warning”)
double d1 = 3.2;
double d2 = 3.9999;
int i1 = static_cast<int>(d1); // i1 = 3
int i2 = static_cast<int>(d2); // i2 = 3
66
Implicit Casting
int i = 3;
double d = 4.8;
double d3 = i / d; // d3 = 0.625 = double(i) / d
int i3 = d3; // i3 = 0 = int(d3)
// Warning! Assignment may lose information
67
Control Flow: If Statement
if (<boolean_exp>)
<true_statement>
[else if (<boolean_exp>)
<else_if_statement>]
[else
<else_statement>]
68
Control Flow: Switch Statement
char command; default :
cin >> command; cout << “Error\n”;
break;
switch (command) { }
case ‘I’ :
editInsert();
break;
case ‘D’ :
editDelete();
break;
case ‘R’ :
editReplace();
break;
69
Control Flow: While & DO-While
while (<boolean_exp>)
<loop_body_statement>
do
<loop_body_statement>
while (<boolean_exp>)
70
Control Flow: For Loop
for ([<initialization>];[<condition>];[<increment>])
<body_statement>
71
Functions, Overloading,
Inline function
72
Functions
bool evenSum (int a[], int n); // function declaration
int main() {
const int listLength = 6;
int list[listLength] = {4, 2, 7, 8, 5, 6};
bool result = evenSum(list, listLength); // call the function
if (result) cout << “even sum.\n”;
else cout << “odd sum.\n”;
return EXIT_SUCCESS;
}
bool evenSum (int a[], int n){ //function definition
int sum = 0;
for (int i = 0; i < n; i++) sum += a[i];
return (sum %2) == 0;
}
73
Function Overloading
•#include<iostream> In C, you can’t
•using namespace std; use the same name for
different functions
•int abs(int n) {
• return n >= 0 ? n : -n;
•} C++ allows multiple
functions with the same
•double abs(double n) { name: the right function
• return (n >= 0 ? n : -n); is determined at runtime
•}
based on argument types
•int main( ) {
• cout << “absolute value of ” << -123;
• cout << “ = ” << abs(-123) << endl;
• cout << “absolute value of ” << -1.23;
• cout << “ = ” << abs(-1.23) << endl;
•}
74
Function Overloading
•#include<iostream> In C, you can’t
•using namespace std; use the same name for
multiple function definitions
•int abs(int n) {
Type matching
• return n >= 0 ? n : -n;
•} C++ allows multiple
functions with the same
•double abs(double n) { Type matching name as long as
• return (n >= 0 ? n : -n); argument types are
•}
different: the right
function is determined at
•int main( ) { runtime based on
• cout << “absolute value of ” << -123;
argument types
• cout << “ = ” << abs(-123) << endl;
• cout << “absolute value of ” << -1.23;
• cout << “ = ” << abs(-1.23) << endl;
•}
75
Polymorphism
• Allow values of different data types to be handled using
a uniform interface.
• One function name, various data types
ü Function overloading
• Merit
ü improve code readability
• Ex.
abs ( ) labs ( ) fabs ( )
C
int long int floating point
abs ( )
C++
int long int floating point
76
Resolving an Overloaded Function Call
Precedence for function calls using arg type
Implicit type conversion by widening
1. An exact match
(char à short à int à long à float à double)
2. A match through promotion
3. A match through application of a type conversion
Implicit type conversion by narrowing
+ Explicit type conversion
void WhichOne ( float f ); // exact match
void WhichOne ( double d ); // promotion
void WhichOne ( int c ); // type conversion
Type Casting in C++
int main( ) { In C, (type_name ) expression
WhichOne (3.5f ); In C++,
(i) the same as in C or
return 0; (ii) type_name may be used as if function name with
argument expression.
} ex: (int ) 1.5 à int (1.5) is ok in C++.
77
Default Arguments (1/2)
#include<iostream> default values
using namespace std;
int calcCubeVolume(int width = 1, int height = 1, int depth = 1);
int main ( ) {
cout << “[def, def, def] ” << calcCubeVolume( ) << endl;
All default values are used.
cout << “[2, def, def] ” << calcCubeVolume(2) << endl;
First arg. overrides the default value.
cout << “[2, 2, def] ” << calcCubeVolume(2, 2) << endl;
First two args. overrides the default values.
cout << “[2, 2, 2] ” << calcCubeVolume(2, 2, 2) << endl;
return 0; All args. overrides the default values.
}
result>
int calcCubeVolume(int width, int height, int depth) { [def, def, def] 1
return (width * height * depth); [2, def, def] 2
}
[2, 2, def] 4
[2, 2, 2] 8
78
Default Arguments (2/2)
Default arguments may be provided for trailing arguments only.
79
Default Args vs. Function Overloading
#include<iostream>
using namespace std;
81
Example : Operator Overloading
#include <iostream>
int main()
using namespace std;
{
enum Day { sun, mon, tue, wed, thu, fri,sat };
Day d = tue;
Operator overloading cout << "current : ";
Day& operator++(Day& d)
print(d);
{
for(int i = 0; i < 6; i++){
return d = (sat == d) ? sun : Day(d+1); use of overloaded operator
++d;
}
}
void print(Day d){
cout << "after 6 days : ";
switch(d){
print(d);
case sun : cout << "sun\n"; break;
return 0;
case mon : cout << "mon\n"; break;
}
case tue : cout << "tue\n"; break;
case wed : cout << "wed\n"; break;
case thu : cout << "thu\n"; break;
case fri : cout << "fri\n"; break; result>
case sat : cout << "sat\n"; break;
}
current : tue
} after 6 days : mon
82
Operator Overloading
Passenger yung, beyonce;
…
…
if (yung == beyonce)
{
…
}
86
Constructor and Destructor
87
Class Structure in General Form
Superclass(es)
data (variables) à member variables for inheritance
operation 2’ default
88
Constructors
• A special, user-defined member function defined within class
ü Initializes member variables with or without arguments
• The function is invoked implicitly by the compiler whenever a class object is defined or
allocated through operator new
class record { class record {
public: public:
char name[MAX]; char name[MAX];
private: private:
int course1, course2; int course1, course2;
same name as class double avg;
double avg;
public: always in “public” to be used by public:
record ( ) { all users for this class record ( );
strcpy(name, ""); void print(void);
course1 = course2 = 100; must not specify a return type };
avg = 100; record::record ( ) {
} Constructor strcpy(name, "");
void print(void); course1 = course2 = 100;
}; avg = 100;
} 89
Default Constructor with No Argument
record::record( ) {
#include<iostream> result>
strcpy(name, "");
using namespace std;
course1 = course2 = 100;
#define MAX 10 course1 = 100, course2 = 100
avg = 100;
avg = 100
}
class record {
public: course1 = 100, course2 = 100
char name[MAX]; int main( ) { avg = 100
private: record myRecord =
int course1, course2; record::record(); course1 = 100, course2 = 100
double avg; record hisRecord = record( ); avg = 100
public: record herRecord;
record( );
void print(void); myRecord.print( );
}; hisRecord.print( ); Same initializations
herRecord.print( );
return 0; ∵implicitly called
void record::print(void)
{…} } without supplying an argument
à Default constructor
90
Constructors with Arguments
#include<iostream> record::record() { void record::print(void) { … }
using namespace std; strcpy(name, "");
#define MAX 10 course1 = course2 = 100; int main( ) {
avg = 100; record myRecord;
class record { } record yourRecord = record("KIM", 80,
public: 100);
char name[MAX]; record::record(char *str, int score) { record hisRecord("LEE", 70);
private: strcpy(name, str);
int course1, course1 = course2 = score; myRecord.print( );
course2; avg = score; yourRecord.print( );
double avg; hisRecord.print( );
}
public:
record(); return 0;
record::record(char *str, int score1, int
record(char*, int); score2) { }
record(char*, int, shorthand notation
strcpy(name, str);
int); course1 = score1; course2 = score2; same as
void print(void); record hisRecord = record(“LEE”, 70);
avg = ((double) (course1 + course2)) /
}; 2.0;
}
overloading
91
Destructors
• A special, user-defined class member function defined in class
• The function is invoked whenever an object of its class goes out of scope or operator
delete is applied to a class pointer
92
Initialization, static, “this”
93
Initialization Style: Vars vs. Class Objects
C C++
#include<stdio.h> #include<iostream>
using namespace std;
result>
10a
94
Initialization of Class Objects as Members
Constructor
#include<iostream> void record::print(void) {
1. Member initialization
using namespace std; cout << id; 2. Assignment
#define MAX 10 cout << " : " << score << endl;
}
class record {
public: int main( ) {
int id; record myRecord(20090001, 70);
int score; myRecord.print( );
result>
record(int i = 0, int s = 100); return 0;
} 20090001 : 70
void print(void);
};
Members, id and score, are
C.f.
objects of class int
record::record(int i, int s) à Initialization by calling record::record(int i, int s)
: id(i), score(s) constructor for class int {
{ and create objects id and score id = i; score = s;
Assignments }
Implicit initialization of class
} objects by constructor for int
95
Global Variable
#include <iostream> result>
using namespace::std; 1th student
2th student
int count = 1; Global Variable
class student{
char name[20];
int age;
public:
student(char* _name, int _age){
strcpy(name, _name);
age = _age;
cout << count++ << "th student" << endl;
}
};
int main()
{
student s1("Kim", 20);
student s2("Seo", 28); u Global variables
return 0; v Undesirable in Object-Oriented concept
} v All functions can access global variables
à Error-prone, hard to debug, etc.
96
Recall: Class Structure in General Form
Superclass(es)
data (variables) à member variables for inheritance
operation 2’ default
97
Static: Per-class variable
#include <iostream>
using namespace::std;
class student{
char name[20]; Static Member Variable = global to all objects created from the student class
int age;
static int count;
public:
student(const char* _name, int _age){
strcpy(name, _name);
age = _age;
cout << count++ << "th student" << endl;
}
};
int main()
{
student s1("Kim", 20); result>
student s2("Seo", 28);
1th student
return 0; 2th student
}
98
The Pointer this
• Reserved keyword
• Inside a member function, how can we access ”my object itself”?
• The address of the class object through which the member function has been invoked
#include<iostream> result>
using namespace std; Object p1
Address of p1 : 0012FED7
class Pointer{
public: this of p1 : 0012FED7
Pointer* GetThis(){ Object p2
return this; Address of p2 : 0012FECB
} this of p2 : 0012FECB
};
int main()
{
Pointer p1;
Pointer p2;
cout << "Object p1" << endl;
cout << "Address of p1: " << &p1 << endl;
cout << "this of p1: " << p1.GetThis() << endl;
cout << "Object p2" << endl;
cout << "Address of p2: " << &p2 << endl;
cout << "this of p2: " << p2.GetThis() << endl;
return 0;
} 99
Example: this Pointer (1/2)
#include<iostream> void point::set(int a, int b) {
using namespace std; this->x = a; this->y = b;
}
class point {
int x, y; void point::print() {
public: cout << "[" << this;
point(int a = 0, int b = 0); cout << "] " << this->x;
void set(int a, int b); cout << ", " << this->y << endl;
void print(); }
};
int main() {
point::point(int a, int b) { point p(1, 1);
this->set(a, b); p.set(2, 2);
} p.print();
return 0;
result>
}
[0xbfec6f00] 2, 2
100
Example: this Pointer (2/2)
#include<iostream> void point::set(int x, int y) {
using namespace std; x = x; y = y;
}
class point { this->x = x; this->y = y;
int x, y; void point::print() {
public: cout << x << "," << y << endl;
point(int a = 0, int b = 0); }
void set(int x, int y); Are x and y arguments or member
variables?
void print(); int main() { priority : arguments > member variables
}; point p(1, 1);
p.set(2, 2);
point::point(int a, int b) { p.print();
x = a; y = b;
} return 0;
} result>
1, 1 à 2, 2
101
Array of Classes
#include<iostream> void record::print(void) {
using namespace std; cout << order << "] ID : " << id
<< endl;
class record { cout << course1 << ”, ” << course2
public: << endl;
static int count; }
int order, id;
int course1, course2; int main( ) {
record(int i = 0, int s1 = 100, int s2 = record students[3]; calls default constructor
100); for (int i = 0; i < 3; i++)
void print(void); students[i].print( );
memory
}; return 0; result>
} students[0]
1] ID : 0
int record::count = 0; 100, 100 students[1]
2] ID : 0 students[2]
record::record(int _id, int s1, int s2) { 100, 100
id = _id; course1 = s1; course2 = s2; 3] ID : 0
order = ++count; 100, 100
}
102
Array of Classes - Initialization
#include<iostream> void record::print(void) {
using namespace std; cout << order << " [ " << id;
cout << " ] score = " << score << endl;
class record { }
public:
static int count; int main( ) {
int order, id, score; record students[3] = { record(20090001, 99),
record(int _id = 0, record(),
int _score = 100); record(20090333) };
void print(void); for (int i = 0; i < 3; i++)
}; students[i].print( );
return 0;
int record::count = 0; }
result>
1 [ 20090001 ] score = 99
record::record(int _id, int _score) {
2 [ 0 ] score = 100
id = _id; score = _score;
3 [ 20090333 ] score = 100
order = ++count;
}
103
Array of Pointers to Classes
#include<iostream> void record::print(void) { ... }
using namespace std;
int main( ) {
class record { record *students[3]; // array of pointers
public: for (int i = 0; i < 3; i++)
static int count; students[i] = new record(2009000 + i, i);
int order, id, score; for (int i = 0; i < 3; i++) {
record(int _id = 0, students[i]->print();
int _score = 100); delete students[i];
void print(void); }
}; return 0;
memory
}
int record::count = 0; students[0] record0
students[1] record1
record::record(int _id, int _score) { result> students[2] record2
id = _id; score = _score; 1 [ 2009000 ] score = 0
order = ++count; 2 [ 2009001 ] score = 1
} 3 [ 2009002 ] score = 2
104
Access Control, Inheritance
105
Access Control
class AccessControl { int main( ) {
public: Access specifier AccessControl ac;
int publicData; public ac.publicData = 1; (O)
void publicFunc( ); ac.publicFunc( ); (O)
protected:
int protectedData; protected ac.protectedData = 2; (X)
void protectedFunc( ); ac.protectedFunc( ); (X)
private:
int privateData; private ac.privateData = 3; (X)
void privateFunc( ); ac.privateFunc( ); (X)
}; };
general users derived classes & friends own & friends
public:
protected:
private:
106
Example: Access Control
#include<iostream> int main( ) {
#define MAX 10 record myrecord;
using namespace std; myrecord.name = “KIM JH”;
myrecord.course1 = 100;
class record{ myrecord.course2 = 90;
int course1, course2; int sum = myrecord.course1 +
public: myrecord.course2;
char name[MAX]; by default, myrecord.avg = ((double) sum) / 2;
private: private myrecord.print( );
double avg; return 0;
public: can be repeated }
void print(void) {
cout << name << endl;
cout << “course1 = ” << course1 Access Error
à How to modify?
<< “, course2 = ” << course2
<< endl;
cout << “avg = ” << avg << endl;
}
};
107
Example: Access Control (cont’d)
#include<iostream>
void record::calculate_avg( ) {
#define MAX 10
int sum = course1 + course2;
using namespace std;
avg = ((double) sum) / 2;
}
class record{
public:
int main( ) {
char name[MAX];
record myrecord;
private:
myrecord.name = “KIM JH”;
int course1, course2;
myrecord.set_course1(100);
double avg;
myrecord.set_course2(90);
public:
myrecord.calculate_avg( );
void print(void); // def. is ommitted.
myrecord.print( );
void set_course1(int score) { course1 = score; }
return 0;
void set_course2(int score) { course2 = score; }
}
void calculate_avg( );
};
provide interface to
access the private
vars and function
108
Inheritance
109
Recall: What is Object ?
• Class (« Type in C )
ü Defines the abstract characteristics of a thing (object)
w attributes (data) + behaviors (operations = methods)
• Object (« Variable in C )
ü A pattern (exemplar) of a class
• Instance
ü The actual object created at runtime
ü State: the set of values of the attributes of a particular object
• Methods Car
ü functions Class Attributes
: color, capacity, max. speed, …
Methods
Ferrari Hyundai : accelerate, brake, steer left,
Object steer right, …
(Instance at runtime)
110
Recall: Class Declaration
class_name instance_name1, instance_name2;
C.f. struct tag_name struct_variable, … ;
class_name
instantiation instance_name2
variables
variables
methods
instance_name1 methods
variables
methods
111
Inheritance (1/2)
• Subclassing: define a class based on another class
ü Another class = parent class (or superclass)
ü New class = child class (subclass)
ü Hierarchical classification in a tree form
ü Another way of ”polymorphism”
vehicle
More specialized
(overridden, detailed,
car truck bus
added)
• overrides information in superclass
Superclass à
subclass • refines information in superclass to detailed one
• adds more information to one in superclass
112
Inheritance (2/2)
• Inheritance
ü Inherits data (attributes) and operations (behaviors) from parent
ü + own data and operations
parent_class
ü Increases reusability by inheritance
data 0
operation 0
data 0 data 0
Eyes but operation 0 operation 0
different
data1 data 2
for each class operation 2
operation 1
Sub classes
113
Class Example
/* Fish Class */
class CFish {
int color;
char *name;
int posx, posy;
public:
void setcolor(int color); CJellyFish jelly;
int getcolor (void); CSquid squid;
int setname(const char *name);
void move(int x, int y); jelly.setname(“Jelly Fish”);
};
jelly.setcolor(WHITE_COLOR);
jelly.move(10, 10);
class CJellyFish : public CFish { jelly.turnlight(LIGHT_ON);
int light;
public: squid.setname(“Squid”);
int turnlight(int on);
squid.setcolor(GREY_COLOR);
}; squid.move(40, 20);
squid.setink_color(BLACK_COLOR);
class CSquid : public CFish { squid.produce_ink();
int ink_color;
public:
void setink_color(int color);
int produce_ink(void);
}
114
Inheritance: Mechanism for Reuse
Base class Person class Person {
(Parent, Superclass) name char *name, *telephone, *address;
telephone ...
Derived class
address };
(Child, subclass)
…
Student Professor
class Student : public Person { name name class Professor : public Person {
int studentID, attendance; telephone telephone char *office, *officeHour;
char grade; address ...
address
... …
};
…
}; student ID office
attendance office hour
grade …
…
115
Inheritance: Construct, Destruct Order
u Constructor order
base class derived class
u Destructor order
derived class base class int main( ) {
Child child;
return 0;
class Parent { }
public:
Parent( ) { cout<<"Parent( )"<<endl; }
~Parent( ) { cout<<"~Parent( )"<<endl; }
};
result >
Parent( )
class Child : public Parent { Child()
public: ~Child()
Child( ) { cout<<"Child( )"<<endl; } ~Parent()
~Child( ) { cout<<"~Child( )"<<endl; }
};
116
Example : Constructors of Derived Class
#include<iostream> class Child : public Parent { int main() {
using namespace std; int _age; Child myRecord("KIM", 21);
public: myRecord.print();
class Parent { int age() { return _age; } return 0;
public: Child(char *name = "", int age = 0); }
char *_name; void print();
char* name() { return _name; } };
Parent(char *name = "");
~Parent() { delete _name; } Child::Child(char *name, int age) :
}; Parent(name)
{
_age = age; careful of arguments
Parent::Parent(char *name) {
_name = new }
char[strlen(name)+1]; uses Member Initialization List
strcpy(_name, name); void Child::print() {
} cout << "Name : " << _name << endl; result>
cout << "age: " << _age << endl; Name : KIM
} age: 21
117
Constructors of Derived Class
• If a base class has constructors, then a constructor must be
invoked
ü Base class acts exactly like a member of the derived class in the
constructor
w base class’ constructor is invoked in Member initialization list
ü Default constructors can be invoked implicitly
118
Access to Base Classes
• Access control of a base class
ü public derivation
ü private derivation
Class B
ü protected derivation
public:
protected:
private:
public derivation protected derivation
private derivation
123
Type Conversion of Pointer & Reference
Object of Subclass derivation
ß Object of Superclass Person Student
name name
Rule telephone
telephone
Object of Superclass address address
ß Object of Subclass student ID
superclass
grade
subclass
p1 = &s1;
s1 = &p1; Person *p1; Student s1;
Student *s1; Person p1;
Person Student
name p1->name “KIM”
s1->name
5454
telephone
p1->address #3109
address
2009001
s1->grade
? A
124
Overriding: From Subclass to Superclass
derivation
Parent Child
name name
Work( ) Work( )
125
Example: Overriding (1/2)
#include<iostream>
using namespace std; int main() {
Child child;
class Parent { child.print( );
public: return 0;
void print( ) { }
cout << "I'm your father." << endl;
}
}; result>
overriding
I'm your son.
class Child : public Parent {
public:
void print( ) {
cout << "I'm your son." << endl;
}
};
126
Example: Overriding (2/2)
#include<iostream>
int main() {
using namespace std;
Child child;
child.print( );
class Parent {
child.print(3);
public:
return 0;
void print( ) {
}
cout << "I'm your father." << endl;
}
};
overriding result>
class Child : public Parent { I'm your son.
public: I'm your son.
void print(int i = 1) { I'm your son.
for (int j = 0; j < i; j++) I'm your son.
cout << "I'm your son." << endl;
}
};
127
Call Overridden Functions
#include<iostream>
int main() {
using namespace std;
Child child;
child.print( );
class Parent {
child.Parent::print( );
public:
return 0;
void print( ) {
}
cout << "I'm your father." << endl;
}
};
overriding result>
class Child : public Parent { I'm your son.
public: I'm your father.
void print( ) {
cout << "I'm your son." << endl;
}
};
128
Static Binding
#include<iostream> int main() {
using namespace std; Child *child = new Child( );
child->print( );
class Parent {
public: Parent *father = child;
void print( ) { father->print( ); Static binding
cout << "I'm your father." << endl; (compile-time binding)
} delete child;
};
overriding return 0;
class Child : public Parent { }
public:
void print( ) {
cout << "I'm your son." << endl; result>
} I'm your son. How does father do
};
as child ?
I'm your father.
à Dynamic binding
129
Dynamic Binding: Virtual Functions
#include<iostream> int main() {
using namespace std; Child *child = new Child( );
child->print( );
class Parent {
virtual function
public: Parent *father = child;
virtual void print( ) { father->print( ); Dynamic binding
cout << "I'm your father." << endl; (run-time
} delete child; binding)
}; result>
overriding return 0; I'm your son.
class Child : public Parent { } I'm your son.
public:
void print( ) {
cout << "I'm your son." << endl; u Polymorphism à Ability to have many forms
} - Objects with different internal structures can share
}; the same external interface
- virtual function and class derivation are means to
realize polymorphism
130
Virtual and Non-Virtual Functions
class Parent { Parent father;
public: Child son;
virtual void vpr( ) { cout << ”vpr: parent" << endl; }
void nvpr ( ) { cout << ”nvpr: parent" << endl; } Parent *par_pt = &son
};
131
Virtual Destructor (1/2)
#include <iostream> class Child : public Parent { int main() {
using namespace std; char* name; Parent *parent = new Child("KIM", "JH");
public: Child *child = new Child("KIM", "HS");
class Parent { Child(char* _familyName, char* parent->PrintName();
char* familyName; _name) child->PrintName();
public: : Parent(_familyName) { cout << endl;
Parent(char* _familyName) { name = new char[strlen(_name)+1]; delete child;
familyName = new strcpy(name, _name); cout << endl;
char[strlen(_familyName)+1]; } delete parent;
strcpy(familyName, _familyName); ~Child( ){
} cout << "~Child()" << endl; return 0;
How to delete
delete name; } child’s name?
~Parent( ){ }
cout << "~Parent()" << endl; virtual void PrintName() {
Parent::PrintName(); result>
delete familyName;
cout << name << endl; KIM,JH
}
} KIM,HS
virtual void PrintName() {
cout << familyName << ','; };
~Child()
}
~Parent()
};
~Parent()
132
Virtual Destructor (2/2)
#include <iostream> class Child : public Parent { int main() {
using namespace std; char* name; Parent *parent = new Child("KIM", "JH");
public: Child *child = new Child("KIM", "HS");
class Parent { Child(char* _familyName, char* parent->PrintName();
char* familyName; _name) : Parent(_familyName) { child->PrintName();
public: name = new cout << endl;
Parent(char* _familyName) { char[strlen(_name)+1]; delete child;
familyName = new strcpy(name, _name); cout << endl;
char[strlen(_familyName)+1]; } delete parent;
strcpy(familyName, _familyName); ~Child( ){
} cout << "~Child()" << endl; return 0;
virtual ~Parent( ){ delete name; }
cout << "~Parent()" << endl; }
result>
delete familyName; virtual void PrintName() {
KIM,JH
Parent::PrintName();
} KIM,HS
virtual void PrintName() { cout << name << endl;
cout << familyName << ','; }
~Child()
} };
~Parent()
};
~Child()
~Parent()
133
Template: Function and Class
134
Function Template (1)
135
Function Template (2)
136
Function Overloading vs. Function Template
• Function overloading •#include<iostream>
•using namespace std;
ü Same function name, but
different function prototypes •int abs(int n) {
• return n >= 0 ? n : -n;
ü These functions do not have •}
• Function template •
•
cout << “ = ” << abs(-123) << endl;
cout << “absolute value of ” << -
ü Same code piece, which 1.23;
• cout << “ = ” << abs(-1.23)
applies to only different types << endl;
•}
137
Class Template (1)
• In addition to function, we can define a generic template
class
• Example: BasicVector
ü Stores a vector of elements
ü Can access i-th element using [] just like an array
138
Class Template (2)
• BasicVector
ü Constructor code?
• How to use?
139
Class Template (3)
• The actual argument in the instantiation of a class template
can itself be a templated type
• Example: Twodimensional array of int
140
Exceptions
141
Exceptions: Intro
• Exception
ü Unexpected event, e.g., divide by zero
ü Can be user-defined, e.g., input of studentID > 1000
ü In C++, exception is said to be “thrown”
ü A thrown exception is said to be “caught” by other code
(exception handler)
ü In C, we often check the value of a variable or the return value
of a function, and if… else… handles exceptions
w Dirty, inconvenient, hard to read
142
Exception: Also a class
143
Exception: Throwing and Catching
144
Exception Example (1)
•#include <iostream>
•using namespace std;
•double division(int a, int b){
• if( b == 0 ) {
• throw "Division by zero condition!";
• }
• return (a/b);
•}
•int main () {
• int x = 50; int y = 0; double z = 0;
• try {
• z = division(x, y);
• cout << z << endl;
• } catch (const char* msg) {
• cerr << msg << endl;
• }
• return 0;
•}
145
Exception Specification
• In declaring a function, we should also specify the
exceptions it might throw
ü Lets users know what to expect
146
Exception: Any Exception and No Exception
147
C++ Standard Exceptions
148
Exception Example (2)
•#include <iostream>
•#include <exception>
•using namespace std;
•int main()
•{
• try {
• throw MyException();
• }catch(MyException& e){
• std::cout << "MyException caught" << std::endl;
• std::cout << e.what() << std::endl;
• } catch(std::exception& e){
• //Other errors
• }
•}
149
Friend
150
Recall: Access Control
class AccessControl { int main( ) {
public: AccessControl ac;
int publicData; public ac.publicData = 1; (O)
void publicFunc( ); ac.publicFunc( ); (O)
protected:
int protectedData; protected ac.protectedData = 2; (X)
void protectedFunc( ); ac.protectedFunc( ); (X)
private:
int privateData; private ac.privateData = 3; (X)
void privateFunc( ); ac.privateFunc( ); (X)
}; };
general users derived classes & friends own & friends
public:
protected:
private:
151
Friends to a Class
• In some cases, information-hiding is too prohibitive.
ü Only public members of a class are accessible by non-members of the class
• “friend” keyword
ü To give nonmembers of a class access to the nonpublic members of the class
• Friend
ü Functions
ü Classes
class CCC
152
Example: Friend Functions
#include<iostream> void point::print() {
using namespace std; cout << x << ", " << y << endl;
} call-by-reference
class point {
int x, y; void set(point &pt, int a, int b) {
public: pt.x = a; pt.y = b;
point(int a = 0, int b = 0); }
void print();
int main() {
friend void set(point &pt, int a, int b); point p(1, 1);
}; p.print();
set(p, 2, 2); not “p.set( );”
point::point(int a, int b) { p.print();
x = a; y = b;
result>
} return 0;
not member function, 1, 1
but friend function }
2, 2
153
Friend Class
#include<iostream> class rectangle { void rectangle::print() {
using namespace std; point leftTop, rightBottom; cout << "LT:" << leftTop.x;
public: cout << "," << leftTop.y << endl;
class point { void setLT(point pt); cout << "RB:" << rightBottom.x;
int x, y; void setRB(point pt); cout << "," << rightBottom.y << endl;
friend class rectangle; void print(); }
public: };
void set(int a, int b); int main() {
}; void rectangle::setLT(point pt) { rectangle sq;
leftTop.set(pt.x, pt.y); point lt, rb;
void point::set(int a, int b) { } lt.set(1, 1); sq.setLT(lt);
x = a; y = b; rb.set(9, 9); sq.setRB(rb);
} void rectangle::setRB(point pt) { sq.print();
rightBottom.set(pt.x, pt.y); return 0;
} }
hole m e m be r result>
can access w
point rectangle
LT:1, 1
friend class You’re my friend RB:9, 9
rectangle;
154
Wrap Up
• You may not have a big problem in reading the codes in the
book
• However,
ü Be ready to debug your program
ü Be ready to search more things in Google
ü Be ready to meet “compilation errors”
155
Supplementary Materials
156
Example : Constructors
#include<iostream> record::record( ) {
using namespace std; strcpy(name, "");
#define MAX 10 course1 = course2 = 100;
avg = 100;
class record { } Error
public: ∵member variables in “private”
char name[MAX]; int main( ) {
private: record yourRecord = { "HONG GD", 100, 100 };
int course1, course2; yourRecord.print();
double avg;
public: record myRecord = record::record( );
record( ); myRecord.print( );
void print(void);
}; return 0;
}
void record::print(void) { … }
157
Example: Constructors & Destructors
#include<iostream> record::record(char *str, int s1, int s2) {
using namespace std; name = new char[strlen(str)+1];
strcpy(name, str);
class record { course1 = s1; course2 = s2;
public: avg = ((double) (s1 + s2)) / 2;
char *name; }
private:
int course1, course2; void record::print(void) { … }
double avg;
public: int main( ) {
record(char *str = "", int s1 = 100, int s2 = record *myRecord = new record( );
100); record *yourRecord = new record("KIM", 90,
~record(); 100);
void print(void);
}; myRecord->print( );
yourRecord->print( );
record::~record() {
delete []name; delete myRecord, yourRecord;
}
return 0;
}
158
Constructors with Arg. and Default Values
#include<iostream> void record::print(void) { … } result>
using namespace std;
#define MAX 10 int main( ) { course1 = 100, course2 =
100
record myRecord;
avg = 100
class record { record yourRecord = record("KIM",
90); KIM
public:
record hisRecord = "LEE"; course1 = 90, course2 =
char name[MAX];
90
private:
avg = 90
int course1, course2; myRecord.print( );
LEE
double avg; yourRecord.print( );
course1 = 100, course2 =
public: hisRecord.print( );
100
record(char *str = "", int s = 100); avg = 100
void print(void); return 0;
}; implicitly }call with default values shorthand notation
(default constructor)
record::record(char *str, int score) { same as
strcpy(name, str); record hisRecord = record(“LEE”);
course1 = course2 = score;
avg = score;
}
159
A Special Constructor : Copy Constructor
#include<iostream> void point::print() { result>
using namespace std; cout << x << "," << y << endl; P1 :
} 2,2
class point { P2 :
public: int main() { 1,1
int x, y; point p1(1, 1);
point(int _x, int _y) { point p2(p1);
x =_x; y = _y; p1.set(2, 2);
} cout << "P1 : ";
point(const point &pt) { p1.print();
x = pt.x; y = pt.y; cout << "P2 : ";
} p2.print();
void set(int _x, int _y) {
x = _x; y = _y; return 0;
} }
void print();
}; copy constructor
Syntax : X(const X& X1)
160
Default Copy Constructor
#include<iostream> void point::print() { result>
using namespace std; cout << x << "," << y << endl; P1 :
} 2,2
class point { P2 :
public: int main() { 1,1
int x, y; point p1(1, 1);
point(int _x, int _y) { point p2(p1); same result
x =_x; y = _y; p1.set(2, 2);
} cout << "P1 : ";
void set(int _x, int _y) { p1.print();
x = _x; y = _y; cout << "P2 : ";
default copy constructor
} p2.print();
- simply copies all members implicitly
void print(); }
- can be used without definition
};
161
Limitation of Default Copy Constructor
class record{ int main() {
public: record myRecord;
char *name; record hisRecord = myRecord; calls default copy constructor
char *telephone; … = record hisRecord(myRecord);
int age; }
... “myName”
};
myRecord “myTelephone” hisRecord
myRecord.name hisRecord.name
MY_AGE MY_AGE
Shallow copy
Member variables of an object are two pointers (name and telephone) and one integer
à Copied two pointer variables and one integer variable
à Two points variables point to the same locations as ones in original objects.
One integer variable copies its own.
162
Deep Copy Constructor
“myName”
“myTelephone”
myRecord.name hisRecord.name
myRecord.telephone hisRecord.telephone
MY_AGE
MY_AGE Copying
“myName”
“myTelephone”
Deep copy
163
Example: Deep Copy Constructor
#include<iostream> record::record(const record &_record) { void record::print(void) {
using namespace std; name = new cout << name;
char[strlen(_record.name)+1]; cout << " : " << tel << endl;
class record { strcpy(name, _record.name); }
public: tel = new char[strlen(_record.tel)+1];
char *name; strcpy(tel, _record.tel); int main( ) {
char *tel; } record myRecord("KIM",
record(char *, char *); deep copy "6565");
record(const record &); record::~record() { record hisRecord(myRecord);
~record(); delete name, tel; myRecord.modifyTel("5454");
void modifyTel(char *_tel); } myRecord.print( );
void print(void); hisRecord.print( );
}; void record::modifyTel(char *_tel) { return 0;
delete tel; }
record::record(char *_n, char tel = new char[strlen(_tel)+1];
*_tel) { strcpy(tel, _tel);
result>
name = new char[strlen(_n)+1]; }
strcpy(name, _n);
KIM : 5454
tel = new char[strlen(_tel)+1]; KIM : 6565
strcpy(tel, _tel);
}
164
Static Members
class student {
public:
int id;
per class
static int count;
student::count
Static Members
student(int i = 0); student::printCount( )
(data, function)
static void printCount( );
};
myID
student myID, yourID;
myID.id
myID.count
per object instance
myID.student( ) yourID
myID.printCount( ) yourID.id
yourID.count
per object instance
yourID.student( )
yourID.printCount( )
165
Example: Static Members (1/3)
#include<iostream> void student::printCount() { result>
using namespace std; cout << "count = " << count count = 1
<< endl;
count = 2
class student { }
count = 4
public:
int id;
student(int i = 0); int main() {
static void printCount(); student myID = 20090001;
private: myID.printCount();
static int count; student yourID;
}; myID.printCount(); Static member function
student hisID, herID; can be accessed directly
int student::count = 0; student::printCount(); with class name
}
student::student(int i) { A static data member must be initialized outside the class
id = i; definition in the same manner as a non-member variable
count++; ∵only one copy of static member
}
Access of a static member is syntactically identical
166
Example: Static Members (2/3)
#include<iostream>
using namespace std;
class student {
public:
int id;
int order;
student(int i= count); A static data member can appear as a default argument to
static void printCount(); a member function of the class.
private:
(A non-static member cannot.)
static int count;
};
int student::count = 0;
student::student(int i) {
order = i;
count++;
}
167
Example: Static Members (3/3)
#include<iostream> int math::factorial(int a){
using namespace std; facto = 1; result>
while(a != 0){
class math { facto *= a; sum: 15
a -= 1;
private: } factorial : 24
static int sum; return facto; permutation : 30
static int facto; }
static int permu;
public: int math::permutation(int a, int b){
math(){ permu = 1;
sum = 0; permu = math::factorial(a) / math::factorial(a-
facto = 0; b);
Static Member Variables
permu = 0;
Initialization
} return permu;
static int summation(int a); }
static int factorial(int a);
static int permutation(int a, int b); int main() {
}; int result1, result2, result3;
169
Const Member Variables
#include<iostream> void record::print(void) {
using namespace std; cout << "ID : " << id << endl;
cout << "course1 = " << course1;
class record { cout << ", course2 = " << course2
public: << endl;
const int id; constant }
int course1, course2;
record(int i = 0, int s1 = 100, int s2 = int main( ) {
100); record myRecord(20090001, 90, 100);
void print(void); myRecord.print( );
}; return 0;
}
record::record(int i, int s1, int s2) { assignment Error
id = i; record::record(int i, int s1, int s2)
(not initialization)
course1 = s1; course2 = s2; : id(i)
} {
course1 = s1; course2 = s2;
}
170
Const Member Functions
#include<iostream> void point::set(int a, int b)
using namespace std; const { ERROR
x = a; y = b; ∵x, y are non-constant
class point { }
int x, y;
public: void point::print( ) const {
point(int = 0, int = 0); cout << x << "," << y
void set(int, int) const; << endl;
const Member Function: only applied to const data, not to non-const. data
void print( ) const; }
};
int main( ) {
point::point(int a, int b) { point p(1, 1);
x = a; y = b; p.print();
} ERROR
const point p2(2, 2);
∵A const class object cannot
p2.set(3, 3); invoke non-const member functions.
p2.print();
return 0;
}
171
Reference Member Variables (1/2)
#include<iostream> void record::print(void) {
using namespace std; cout << "ID : " << id << endl;
cout << "course1 = " << course1;
class record { cout << ", course2 = " << course2
public: << endl;
int &id; reference }
int course1, course2;
record(int i = 0, int s1 = 100, int s2 = int main( ) {
100); record myRecord(20090001, 90, 100);
void print(void); myRecord.print( );
}; return 0;
}
record::record(int i, int s1,initialization
int s2) result>
: id(i)
ID : garbage
{
course1 = 90, course2 = 100
course1 = s1; course2 = s2;
}
172
Reference Member Variables (2/2)
#include<iostream> void record::print(void) {
using namespace std; cout << "ID : " << id << endl;
cout << "course1 = " << course1;
class record { cout << ", course2 = " << course2
public: << endl;
int &id; reference }
int course1, course2;
record(int &i, int s1 = 100, int s2 = int main( ) {
100); int common = 20090001;
void print(void); record Record1(common, 90, 100);
}; record Record2(common, 70, 80);
common = 20090002;
record::record(int& i, int initialization
s1, int s2) Record1.print( ); result>
: id(i) Record2.print(); ID : 20090002
{ return 0; course1 = 90, course2 = 100
course1 = s1; course2 = s2; } ID : 20090002
}
course1 = 70, course2 = 80
173
Inheritance VS. Nested Class
Nested Has-a relation A circle has a point.
Inheritance Is-a relation A student is a person.
class circle {
point center;
Nested int radius;
...
point center; };
175
Example: Type Conversion of Pointer (1)
#include <iostream> int main() {
using namespace std; Person *p1 = new Person; (O)
Student *p2 = new Person; (X)
class Person { Undergraduate *p3 = new Person; (X)
public:
void Sleep() { cout<<"Sleep"<<endl; } p1->Sleep();
}; p2->Sleep();
p3->Sleep();
class Student : public Person { return 0;
public: }
void Study() { cout<<"Study"<<endl; }
};
177
Example: Type Conversion of Pointer (3)
#include <iostream> int main() {
using namespace std; Person *p1 = new Person; (O)
Person *p2 = new Student; (O)
class Person { Person *p3 = new Undergraduate; ( O )
public:
void Sleep() { cout<<"Sleep"<<endl; } p1->Sleep();
}; p2->Sleep();
p3->Sleep();
class Student : public Person { return 0;
public: }
void Study() { cout<<"Study"<<endl; }
};
178
Overriding and Overloading
#include<iostream> class Child : public Parent {
using namespace std; public:
void print( ) {
overriding
class Parent { cout << "I'm your son." << endl;
public: }
void print( ) { };
cout << "I'm your father." << endl;
}
int main() {
Overloading void print(int i) {
Child child;
(within class)
for (int j = 0; j < i; j++) child.print( ); ERROR
cout << "I'm your father." << endl; child.print(3);
} return 0;
}; }
179
Multiple Inheritance
• In C++, a class can have more than one immediate bass class
ü Not supported in JAVA
• Multiple Inheritance
ü The use of more than one immediate base class
w Inheritance tree à Inheritance graph with no cycle
ü Usage
w class child : public parent1, public parent2 { … }
ü Combined two unrelated classes together as a part of an
implementation of a third class
ü Conflict of names: Two base classes have a member function with
the same name
w To resolve ambiguity, use following expression
n parent class name : : function()
w Ex. when two parents have the same function A()
ch->A(); // error à Ambiguity for inheritance
ch->parent1::A(); // ok
ch->parent2::A(); // ok 180
Example : Multiple Inheritance
#include<iostream> class IO : public Output, public IntInput
using namespace std; {
public:
class Output IO(){}
{ void Delivery(){
public: sprintf(contents, "%d", number);
Output(){} }
void Print() { cout << contents };
<< endl;}
protected: Result>
char contents[20]; int main() Input : 10
}; {
Output : 10
class IntInput IO *a = new IO();
{ cout << ”Input : ";
public: a->In(); // from IntInput class
IntInput(){} a->Delivery(); // from IO class
void In() { cin >> number; } cout << ”Output : ";
protected: a->Print(); // from Output class
int number; return 0;
}; }
181
Heterogeneous List
• Homogenous List
ü List of objects in the same class (type) à Implementation in array
• Heterogeneous List
ü List of objects in different classes
ü Use points to objects in base class and derived classes à array of pts
ü Uniform interface for objects in different classes
182
Pure Virtual Functions and Abstract Class
#include<iostream>
using namespace std;
class Parent {
public:
virtual void print( ) = 0;
};
Pure virtual function
class Child : public Parent { 1. A virtual function is made “pure” by the initialzer = 0.
public: 2. A virtual function cannot be called within an abstract class.
void print( ) {
cout << "I'm your son." << endl;
}
Abstract class
};
1. A class with one or more pure virtual functions
2. No object from class is created.
3. Means to provide an interface without exposing any
implementation details
183
Example: Pure Virtual Functions
#include<iostream> int main() {
using namespace std; Parent parent;
ERROR
parent.print( );
class Parent { Child child;
public: child.print( );
virtual void print( ) = 0; child.Parent::print( ); ERROR
}; return 0;
}
class Child : public Parent {
public:
∵ Cannot invoke a virtual function
void print( ) {
cout << "I'm your son." << endl; ∵ No objects of an abstract class can be created.
}
};
184