Introduction to C++ : Introduction to C++ 1998. 5. 4.
Pang Min-seok
KAIST Dept. of I.E. SPARCS ’97
noticeme@sparcs.kaist.ac.kr
Today’s Topic : Today’s Topic Let’s taste C++!
Let’s feel Object Oriented Programming!
Let’s look at Class !
What is C++? : What is C++? C++ = C + OOP + etc….
C++ is superset of C.
Enable Object Oriented Programming
Why C++?
- for Java, for Windows Programming
Programming Paradigms : Programming Paradigms Sequential Programming - Fortran, BASIC
Structured Programming - Algol, Pascal, C
Object Oriented Programming - C++, Java
Object Oriented Programming (OOP) : Object Oriented Programming (OOP) for ‘Data’ rather than ‘Function’..
Encapsulation
Polymorphism
Inheritance
Encapsulation : Encapsulation Object = Data + Code
Class = Variables + Functions
Object is a kind of variable itself.
Polymorphism : Polymorphism Polymorphism mean ‘many forms’ from Greek.
One Interface, Multiple Methods..
Function Overloading, Operator Overloading
Ex) abs(), labs(), fabs() -> abs()
Inheritance : Inheritance One object acquire the properties of another.
Hierarchical Classification
Ex) CObject - CCmdTarget - CWnd - CDialog
Structure of Class : Structure of Class class class-name { // accessible by only inside of class
private functions and variables of the class
protected : // accessible by derived classes
protected functions and variables of the class
public : // accessible by outside of class
public functions and variables of the class
}
Example of Class : Example of Class
class myclass {
int a;
public :
void set_a(int num);
int get_a();
}
void myclass::set_a(int num) {
a = num;
}
int myclass::get_a() {
return a;
}
Example of Class (continued) : Example of Class (continued) void main() {
myclass ob1, ob2;
ob1.a = 100; // Error !!
ob1.set_a(10); ob2.set_a(99);
cout << ob1.get_a() << “\n”;
cout << ob2.get_a() << “\n”;
}
Example of Function Overloading : Example of Function Overloading #include
void date(char *date) {
cout << “Date:” << date << “\n”;
}
void date(int month, int day, int year) {
cout << “Date:” << month << “/” << day << “/” << year << “\n”;
}
main() {
int month, day, year;
date(“5/4/98”);
date(5, 4, 98);
cin >> month >> day >> year;
date(month, day, year);
}
Constructor and Destructor : Constructor and Destructor class myclass {
int *a;
public :
myclass(); // constructor
~myclass(); // destructor
set_a(int num);
int get_a();
}
myclass::myclass() { a = new int; }
myclass::~myclass() { delete a; }
void myclass::set_a(int num) { a = num; }
int myclass::get_a() { return a; }
Constructor with Parameters : Constructor with Parameters #include
class myclass {
int a;
public :
myclass(int x); // constructor
int get_a();
}
myclass::myclass(int x) { a = x; }
int myclass::get_a() { return a; }
main() {
myclass ob1; // Error !!
myclass ob2(10);
cout << ob2.get_a() << “\n”;
}