First Program Malik Jahan Khan 1 A Simple Example • Each person in Pakistan is identified by a unique ID • For sake of simplicity, assume that each ID is a simple integer • We don’t need any other attribute of the person • The simplest behavior which a person can exhibit is that it may be assigned (set) a new ID or it may display its ID when requested 2 using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } First OO Program 3 #include<iostream> using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); First OO Program Class Definition 4 First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Attributes of Class (Data Members) 5 First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Class Behavior (Member Functions) 6 First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; //Class Definition must end with a semicolon void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } 7 First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; //Class Definition must end with a semicolon int main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Objects Definition 8 First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; //Class Definition must end with a semicolon void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Using Member Functions 9 Instantiation of Objects Person data: ID methods: setID(id), showID() Class person 1 data: 10 person 2 data: 20 person 3 data: 30 10 Private and Public • Data is concealed (hidden or masked) within a class so that it cannot be accessed mistakenly by functions out side the class • The primary mechanism for hiding data is to put it in class and make it private • Private data or functions can only be accessed from within the class • Public data or functions are accessible from outside the class • Usually, functions are public and data is private 11 Graphics Example #include "msoftcon.h" class circle { private: int xCo, yCo, radius; color fillcolor; fstyle fillstyle; public: void set(int x,int y,int r, color fc, fstyle fs) { xCo=x; yCo=y; radius=r; fillcolor=fc; fillstyle=fs; } void draw() { set_color(fillcolor); set_fill_style(fillstyle); draw_circle(xCo,yCo,radius); } }; int main() { init_graphics(); circle c1,c2,c3; c1.set(15,7,5,cBLUE,X_FILL); c2.set(41,12,7,cRED,O_FILL); c3.set(65,18,4,cGREEN,MEDIUM_FILL); c1.draw(); c2.draw(); c3.draw(); return 0; } 12 Self-Reading • Previous example of Graphics (also given in textbook) • Example of Widget Parts (page 223) • C++ objects as data types (page 226) – Example of Distance • There may be a surprise quiz next time from the stuff which we covered so far ;) 13
© Copyright 2025 Paperzz