160 likes | 178 Views
COP3502: Introduction to Computer Science. Object oriented programming. Yashas Shankar. Object oriented programming. Programming style such that you view data, functions, etc as objects. Human. Right_hand. Left_hand. Right_leg. Left_leg. Body. Object oriented programming. Human.
E N D
COP3502: Introduction to Computer Science Object oriented programming Yashas Shankar
Object oriented programming • Programming style such that you view data, functions, etc as objects Human Right_hand Left_hand Right_leg Left_leg Body
Object oriented programming Human Right_hand Left_hand Right_leg Left_leg Body Left_hand move hand, move finger, swing, etc Left_leg Move forward, kick, etc
Object oriented programming Human Right_hand Left_hand Right_leg Left_leg Body Left_hand move hand, move finger, swing, etc Left_leg Move forward, kick, etc Void Walk() { Left_leg.move_forward; Right_leg.move_forward } Human walk, etc
Objects • An object contains both data and functions • Functions are usually public data where other function can call • Data are usually kept private • Other functions cannot access that data • Only functions in this object and access the data Class football_team{ Public: void touch_down(); Private: int score; }; Increase the score by six points
Objects Class football_team{ Public: void touch_down(); Private: int score; }; Int main() { football_team FSU; FSU.touch_down(); }
Objects Class football_team{ Public: void touch_down(); Private: int score; }; Int main() { football_team FSU; FSU.touch_down(); } void football_team::touch_down{ score = score + 6; }
Last class Object Football_team public private score Get_score Touchdown Extrapoint Fieldgoal
This class Object Football_match Object Object Football_team Football_team
This class Object Football_match private public Football_team Fucntion#1 Fucntion#2 Football_team … Fucntion#n
This class Object Football_match private public Football_teamhome_team Fucntion#1 Fucntion#2 Football_teamaway_team … score Get_score Touchdown Fucntion#n Extrapoint Fieldgoal
This class Object Football_match private public Football_teamhome_team Football_teamaway_team Fucntion#1 Fucntion#2 name … score Get_score Touchdown Fucntion#n Set_name Extrapoint Fieldgoal Get_name
football_team object #include <string> class football_team { public: int get_score(); void touch_down(); void extra_point(); void field_goal(); void set_score(int a); void set_name(string a); string get_name(); private: int score; string name; }; void football_team::set_name(string a) { name = a; } string football_team::get_name() { return name; }
Object Football_match private public Set_name Football_teamhome_team Set_score Print_score Football_teamaway_team Fucntion#1 Home_touchdown name Fucntion#2 Away_touchdown Home_extrapoint … score Get_score Away_extrapoint Touchdown Fucntion#n Set_name Home_fieldgoal Extrapoint Get_name Fieldgoal Away_fieldgoal
How to access elements in an object Football_match public Assume public Private Set_name Football_teamaway_team Set_score score Touchdown Main() { Football_match match1; match1.Set_name(..); match1.Set_score(..); match1.away_team.Touchdown(); match1.away_team.Set_name(..); match1.away_team.score = 7; match1.away_team.name = “Miami”; } Set_name name Access Denied
private public Football_match Football_teamhome_team home_touchdown Football_teamaway_team set_score int football_match::home_touchdown() { home_team.touch_down(); } name int football_match::set_score(int a, int b) { home_team.set_score(a); away_team.set_score(b); } Set_name score Get_score Get_name Main() { Football_match match1; set_score(0,0); home_touchdown(); { Touch_down Extrapoint Fieldgoal