C++ Programming: chapter 3 - class

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
CPS 235 Object Oriented Programming Paradigm
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Operator overloading redefine the operations of operators
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 21.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 Lab Session-9 CSIT221 Fall 2002 Lab Exercise Based on operator overloading (Demo Required)
Lecture 9 Concepts of Programming Languages
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
C++ Review (3) Structs, Classes, Data Abstraction.
Inheritance One of the most powerful features of C++
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
C++ Programming: chapter 3 - class 2015, Spring Pusan National University Ki-Joune Li 1.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
C++ Programming: chapter 4 – operator overloading 2014, Spring Pusan National University Ki-Joune Li 1.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Chapter 2 Objects and Classes
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
OBJECT ORIENTED PROGRAMMING
Chapter 5 Classes.
Chapter 2 Objects and Classes
group work #hifiTeam
Polymorphism Lec
Lecture 9 Concepts of Programming Languages
Classes and Data Abstraction
Classes & Objects: Examples
תכנות מכוון עצמים ו- C++ יחידה 06 העמסת אופרטורים
Pointers & Functions.
Classes Short Review of Topics already covered Constructor
C++ Programming: chapter 4 – operator overloading
Summary: Abstract Data Type
C++ Constructor Insanity CSE 333 Summer 2018
C++ Constructor Insanity CSE 333 Winter 2019
Pointers & Functions.
CS 144 Advanced C++ Programming February 21 Class Meeting
Class: Special Topics Overloading (methods) Copy Constructors
C++ Constructor Insanity CSE 333 Spring 2019
Constructors & Destructors
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

C++ Programming: chapter 3 - class 2018, Spring Pusan National University Ki-Joune Li http://lik.pnu.kr

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)

Implementation Interfaces to outside Internal member functions Internal member functions Internal member functions Member Data (States) Member Data (States) Member Data (States)

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; }

Private: Encapsulation을 위하여 내부의 내용을 감춤 Private versus Public Private: Encapsulation을 위하여 내부의 내용을 감춤 Class의 내부에서만 사용가능 Scope resolution operation Friend Inheritance Public Class의 외부에서 누구나 사용가능 – struct와 유사 주로 외부에 노출되는 interface 함수

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; ?

#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 ?

? #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; ?

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의 차이는 ?

가장 기본적인 Member Functions Constructor/Destructor Set/Get Functions Operators Search/Insert/Delete/Update

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”);

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”);

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(); }

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(); }

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(); }

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(); } 언제 호출되는가?

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로 하면 어떻게 되는가?

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;  } +로 정의되지 않았는데??

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; }