Download presentation
Presentation is loading. Please wait.
1
C++ Programming: chapter 3 - class
2018, Spring Pusan National University Ki-Joune Li
2
black box (hiding the internal details)
- member functions - interfaces - methods Class black box (hiding the internal details) ? Object (or Instance) Object (or Instance) Object (or Instance)
3
Implementation Interfaces to outside Internal member functions Internal member functions Internal member functions Member Data (States) Member Data (States) Member Data (States)
4
Scope Resolution Operator
#include <iostream> using namespace std; class Point { private: int X; int Y; int Z; public: void setX(int a); void setY(int b); void setZ(int c); void printout() { cout<< “(“ << X << “, “<<Y<<“, “<<Z<<“)\n”; } int volume(); }; int main() { Point myPoint; int vol; myPoint.setX(7); myPoint.setY(16); myPoint.setZ(21); vol = myPoint.volume(); myPoint.printout(); cout << vol; return 0; } Class Definition Object Scope Resolution Operator int Point::volume() { int result; result=X*Y*Z; return result; }
5
Private: Encapsulation을 위하여 내부의 내용을 감춤
Private versus Public Private: Encapsulation을 위하여 내부의 내용을 감춤 Class의 내부에서만 사용가능 Scope resolution operation Friend Inheritance Public Class의 외부에서 누구나 사용가능 – struct와 유사 주로 외부에 노출되는 interface 함수
6
Function overloading 동일한 이름의 함수를 Parameter 수와 Type으로 Binding ?
#include <iostream> using namespace std; class Point { private: int X; int Y; int Z; public: void printout() { cout<< “(“ << X << “, “<<Y<<“, “<<Z<<“)\n”; } void setValues() {X=Y=Z=0;} void setValues(int a); void setValues(float a); void setValues(int a, int b, int c); }; int main() { Point myPoint; float y=10.0; myPoint.setValues(); myPoint.setValues(y); myPoint.printout(); return 0; } int Point::setValues(int m) { X=Y=Z=m; } int Point ::setValues(float x) { X=Y=Z=static_case<int>(x); int Point ::setValues(int a, int b, int c) { X=a; Y=b; Z=c; ?
7
#include <iostream> using namespace std; class Point {
private: int X; int Y; int Z; public: void printout() { cout<< “(“ << X << “, “<<Y<<“, “<<Z<<“)\n”; } void setValues(int a); void setValues(float a); void setValues(int a, int b, int c); }; int main() { Point myPoint; my Point.setValues(); my Point.printout(); return 0; } int Point::setValues(int m) { X=Y=Z=m; } int Point::setValues(float x) { X=Y=Z=static_case<int>(x); int Point::setValues(int a=0, int b=0, int c=0) { X=a; Y=b; Z=c; Default values ?
8
? #include <iostream> using namespace std; class Point {
private: int X; int Y; int Z; public: void printout() { cout<< “(“ << X << “, “<<Y<<“, “<<Z<<“)\n”; } void setValues() {X=Y=Z=0;} void setValues(float a); void setValues(int a, int b, int c); }; int main() { Point myPoint; int a=10; myPoint.setValues(a); myPoint.printout(); return 0; } int Point::setValues(float x) { X=Y=Z=static_case<int>(x); } int Point::setValues(int a, int b, int c) { X=a; Y=b; Z=c; ?
9
Call-By Reference &을 이용하면 Parameter passing 때 Call-By reference 가능
Cf. call-by value #include <iostream> using namespace std; class Point { private: int X; int Y; int Z; public: void printout() { cout<< “(“ << X << “, “<<Y<<“, “<<Z<<“)\n”; } int setVaues(int &); void setValues(int a, int b, int c); }; int main() { Point Point; int a=10; myPoint.setValues(a); cout << “a= “ <<a<<endl; myPoint.setValues(30); //?? return 0; } int Point::setValues(int & m) { X=Y=Z=m++; } Call-by reference와 call-by value의 차이는 ?
10
가장 기본적인 Member Functions
Constructor/Destructor Set/Get Functions Operators Search/Insert/Delete/Update
11
Class Constructor Class Constructor Object Object
#include <iostream> using namespace std; class Point { private: int X; int Y; int Z; public: Point(); Point a, int b, int c); printValues() { cout << “X=“<<X<<“, Y=“<<Y<<“, Z=“<<Z<<“\n”;} }; int main() { Point myPoint; Point yourPoint(1,2,3); return 0; } Object Object Point::Point() { X=Y=Z=0; cout << “Construction with default values\n”; printValues(); } Point::Point(int a, int b, int c) { X=a; Y=b; Z=c; cout << “Construction with parameterized values\n”);
12
new operator Class Constructor Object Object
#include <iostream> using namespace std; class Point { private: int X; int Y; int Z; public: Point(); Point(int a, int b, int c); printValues() { cout << “X=“<<X<<“, Y=“<<Y<<“, Z=“<<Z<<“\n”;} }; int main() { Point *myPointPtr=new Point; Point *yourPointPtr=new Point(1,2,3); Point *myPoints=new Point[10]; return 0; } Object Object Point::Point() { X=Y=Z=0; cout << “Construction with default values\n”; printValues(); } Point::Point(int a, int b, int c) { X=a; Y=b; Z=c; cout << “Construction with parameterized values\n”);
13
Class Destructor Class Constructor Object Destructor
#include <iostream> using namespace std; class Point { private: int X; int Y; int Z; public: Point(); ~Point(); Point (int a, int b, int c); printValues() { cout << “X=“<<X<<“, Y=“<<Y<<“, Z=“<<Z<<“\n”;} }; int main() { Point myPoint; Point yourPoint(1,2,3); return 0; } Constructor Object Destructor Point::~Point() { cout << “Construction with default values\n”; printValues(); }
14
delete operator Class Constructor Object Destructor
#include <iostream> using namespace std; class Point { private: int X, Y, Z; public: Point(); ~Point(); Point a, int b, int c); printValues() { cout << “X=“<<X<<“, Y=“<<Y<<“, Z=“<<Z<<“\n”;} }; int main() { Point *myPointPtr=new Point; Point *myPoints=new Point[10]; delete myPointPtr; delete[] myPoints;’ return 0; } Object Destructor Point::~Point() { cout << “Construction with default values\n”; printValues(); }
15
Copy Constructor Class Constructor Object Object
#include <iostream> using namespace std; class Point { private: int X, Y, Z; public: Point(); ~Point(); Point(const Point &a); printValues() { cout << “X=“<<X<<“, Y=“<<Y<<“, Z=“<<Z<<“\n”;} }; int main() { Point firstPoint(1,2,3); Point secondPoint(firstPoint); Point myPoint(5,6,7); functionA(myPoint); return 0; } functionA(Point m) { m.printValues(); } Constructor Object Object Point::Point(Point & a) { X=a.X; Y=a.Y; Z=a.Z; cout << “Copy Construction\n”; printValues(); }
16
Copy Constructor 언제 호출되는가?
#include <iostream> using namespace std; class Point{ private: float X; float Y; public: Point(float a): X(a),Y(a) {}; Point(const Point& a); printValues() { cout << “(“<<X<<“, “<<Y<<“)\n”; } friend Point operator+(const Point&a, const Point& b); }; int main() { Point myPoint(1.0); Point yourPoint(2.0); (myPoint+yourPoint).printValues; return 0; } Point operator+(const Point& a, const Point& b) { Point temp; temp.X=a.X+b.X; temp.Y=a.Y+b.Y; return temp; } Point::Point(const Point & a) { X=a.X; Y=a.Y; cout << “Copy Construction\n”; printValues(); } 언제 호출되는가?
17
Const return object 를 reference로 하면 어떻게 되는가?
#include <iostream> using namespace std; class Point{ private: float X; float Y; public: Point(float a): X(a),Y(a) {}; Point(const Point& a); void printValues() const; friend Point operator+(const Point&a, const Point& b); }; int main() { Point myPoint(1.0); Point yourPoint(2.0); (myPoint+yourPoint).printValues; return 0; } Point operator+(const Point& a, const Point& b) { Point temp; temp.X=a.X+b.X; temp.Y=a.Y+b.Y; return temp; } void printValues() const { cout << “(“<<X<<“, “<<Y<<“)\n”; } Point &operator+(const Point& a, const Point& b) { Point temp; temp.X=a.X+b.X; temp.Y=a.Y+b.Y; return temp; } return object 를 reference로 하면 어떻게 되는가?
18
Constructor와 type casting
Dollar operator+(const Dollar& a, const Dollar& b) { Dollar temp; temp.dollarPart=a.dollarPart+b.dollarPart; temp.centPart=a.centPart+b.centPart; return temp; } #include <iostream> using namespace std; class Dollar{ private: int dollarPart; int centPart; public: Point(float a){dollarPart=a; centPart=a/100;} Point(int a):dollarPart(a),centPart(0){} Point(int a,int b):dollarPart(a),centPart(b){} friend Dollar operator+(const Dollar& a, const Dollar& b); printValues(){ cout<<“$”<<dollarPart<<“.”<<centPart<<endl; }; int main() { Dollar myMoney(1.50); Dollar yourMoney(3,20); (myMoney+yourMoney).printValues(); (myMoney+20).printValues(); (yourMoney+10.50).printValues(); return 0; } +로 정의되지 않았는데??
19
static member, static member fuction
#include <iostream> using namespace std; class Point{ private: float X; float Y; static int count; public: Point(float a): X(a),Y(a) {++count}; Point(const Point& a); static void printCount(); printValues() { cout << “(“<<X<<“, “<<Y<<“)\n”; } friend Point operator+(const Point&a, const Point& b); }; int Point::count=0; int main() { Point myPoint(1.0); Point yourPoint(2.0); (myPoint+yourPoint).printValues(); Point::printCount(); return 0; } void Point::printCount() { cout << “Count=“<<count<<endl; } Point operator+(const Point& a, const Point& b) { Point temp; temp.X=a.X+b.X; temp.Y=a.Y+b.Y; return temp; } Point::Point(const Point & a) { X=a.X; Y=a.Y; ++count; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.