Lecture 9 Class include iostream h class money

  • Slides: 25
Download presentation
Lecture 9 Class )คลาส(

Lecture 9 Class )คลาส(

ตวอยางการนำเอา ไปใชงาน #include "iostream. h" class เรยก money และ add class Human{ วาเปนสมาชก (memb

ตวอยางการนำเอา ไปใชงาน #include "iostream. h" class เรยก money และ add class Human{ วาเปนสมาชก (memb public: ers) ของ class ชอ int money; Human int add)int x, int y( } แต money และ add int result; ตางกนคอ money result = x+y; เปนตวแปรสวน add return result; เปนฟงกชน { ; { เราเลยเรยก money void main)void}( วาเปนตวแปรสมาชก int value; (member variable( Human Ob 1; Ob 1. money=100; และเรยก add value=Ob 1. add(5, 6; ( วาเปนฟงกชนสมาช cout<<Ob 1. money<<"t"<<dd<<endl; ก (member function( {

หนา 14 ไม work ลองแกเปน class Human { public: double money; int result; void

หนา 14 ไม work ลองแกเปน class Human { public: double money; int result; void add(int x, int y); void sub(int x, int y); private: int x, y; }; void Human: : add(int x, int y) { result = x+y; } void Human: : sub(int x, int y) { result = x-y; } (ไม workหรอกครบ (แตเพราะอะไร ทำไมแกแบบนแลว work นะ ครบ

class Human { public: double money; int result; void add(int x, int y); void

class Human { public: double money; int result; void add(int x, int y); void sub(int x, int y); void print(); private: int x, y; }; void Human: : add(int x, int y){ result = x+y; } void Human: : sub(int x, int y){ result = x-y; } void Human: : print(){ cout<<“n”<<result<<endl; } เพมใหผม แสดงผลไดอ กดวย

void main(void) { int value; Human Ob 1; Ob 1. money=100; Ob 1. add(5,

void main(void) { int value; Human Ob 1; Ob 1. money=100; Ob 1. add(5, 6); Ob 1. print(); } สวนการบวก สวนการแสดงผล

#include "iostream. h" class Human{ public: int result; int money; void add)int x, int

#include "iostream. h" class Human{ public: int result; int money; void add)int x, int y; ( void sub)int x, int y; ( void print ; () Human(){ money ; 0= } ~Human(){cout<<"Object is Date"<<"n"; } private: int x, y; ; { void Human: : sub(int x, int y}( result = x-y; { void Human: : add(int x, int y}( result = x+y; { void Human: : print}() cout<<"n"<<money<<"n; " { void main)void( } { Human Ob 1; Ob 1. money=100; Ob 1. print; () Ob 1. money=200; Ob 1. print; () Ob 1. money=300; Ob 1. print; ()

การสบทอดคลาส )class derive ) การสบทอดคลาส กกระทำไดโดยใชคำสงดงตอไปน class myderivedclass: public myclass } private: int myweight

การสบทอดคลาส )class derive ) การสบทอดคลาส กกระทำไดโดยใชคำสงดงตอไปน class myderivedclass: public myclass } private: int myweight ; protected: int setmyweight(int newweight ; ( public: int readmyweight(void ; ( ; {