面向对象编程:C++课件解析与解决方案探讨

需积分: 0 0 下载量 84 浏览量 更新于2024-08-19 收藏 1.21MB PPT 举报
"面向对象程序设计的课程讲解,涉及C++语言和OPP2010的相关知识,包括类的设计、面向对象的概念以及C++与C的特性比较。课程探讨了过程语言的局限性,引出面向对象设计的重要性,同时提到了课程的重点内容、与其他编程范式的关联以及就业面试的相关准备。" 在面向对象程序设计(OPP)中,C++是一种关键的语言,它在C的基础上增加了对面向对象特性的支持。C++的特点在于它既保留了C语言的低级特性,也引入了丰富的构造数据类型和面向对象的语法,如类和对象。然而,相比其他如Java或C#,C++有时被认为不够“纯”面向对象,因为它允许混合使用面向过程和面向对象的编程风格。 在描述中提到的代码示例展示了如何通过类型域方案来实现多态性,但这可能导致类设计与使用者之间的耦合,破坏了封装原则。`fn`函数通过`switch`语句判断`Student`对象的`type`属性来调用相应的显示方法。这种方式虽然能工作,但不是标准的面向对象做法,如使用虚函数和继承可以更好地实现多态,并提高代码的可维护性和扩展性。 在面向对象设计中,类是核心,它们代表现实世界中的实体或概念,而对象则是这些类的实例。类定义了对象的属性(数据成员)和行为(成员函数)。抽象、继承和组合是面向对象的三大特性。抽象允许我们创建抽象类来定义接口,而具体的实现则由子类提供。继承使得子类可以继承父类的属性和行为,从而实现代码重用。组合则是将对象组合成更复杂的结构,增强了灵活性。 课程内容涵盖了C语言的简单回顾,如指针的使用,数组操作等,以及C++中新增的面向对象特性,如类、对象、抽象、继承和组合。此外,课程还强调了面向对象思维的重要性,以及它与其他编程范式(如面向组件、面向方面、面向服务)的关系,以及在就业笔试和面试中的应用。 对于C语言中的数组,数组名是一个指向其首元素的指针,可以传递给函数作为参数。例如,在`Main()`函数中,`read(int a[10])`实质上等同于`read(int *a)`。数组名做参数时,需要注意指针运算和数组访问的边界问题,以防止越界访问,因为越界访问可能导致程序崩溃或数据损坏。 这个课程深入浅出地介绍了C++的面向对象特性,帮助学生理解如何利用这些特性进行有效且易于维护的软件设计。同时,课程也提醒学生注意面向对象设计中的最佳实践,避免不必要的耦合,提高代码的可读性和可扩展性。

#include "def.h" #include "utils/maths.h" //用户注意;接口需要如下声明 extern "C"_declspec(dllexport) PlayerTask player_plan(const WorldModel* model, int robot_id); enum ball_near //PenaltyArea { outOfOrbit, onOrbit, shoot }; PlayerTask player_plan(const WorldModel* model, int robot_id){ PlayerTask task; const point2f& opp_goal = model->get_place_pos(); const float pi = 3.1415926; const float& circleR = 30; const float& DetAngle = 0.6; const point2f& goal = FieldPoint::Goal_Center_Point; const point2f& ball = model->get_ball_pos(); const point2f& kicker = model->get_our_player_pos(robot_id); const float& dir = model->get_our_player_dir(robot_id); ball_near orbit; point2f shootPosOnOrbit = ball + Maths::vector2polar(circleR, (ball - opp_goal).angle()); float toShootDir = fabs((kicker - ball).angle() - (ball - opp_goal).angle()); //(kicker - shootPosOnOrbit).length(); float toBallDist = (kicker - ball).length(); float toOppGoalDir = (opp_goal - kicker).angle(); float toBallDir = (ball - kicker).angle(); point2f robotBallAcrossCirclePoint = ball + Maths::vector2polar(circleR, (kicker - ball).angle()); point2f AntishootPosOnOrbit = ball + Maths::vector2polar(circleR, (opp_goal - ball).angle()); point2f BallToRobot = kicker - ball; if (toBallDist >circleR + 10) orbit = outOfOrbit; else if (toShootDir > 1) orbit = onOrbit; else orbit = shoot; bool getBall = toBallDist < 10; float diffdir_onorbit = 0; float b2r = BallToRobot.angle(); float o2b = (ball - opp_goal).angle(); bool add; switch (orbit) { case outOfOrbit: task.target_pos = robotBallAcrossCirclePoint; task.orientate = toOppGoalDir; break; case onOrbit: if (b2r * o2b >0){ if (b2r > 0){ if (b2r > o2b) add = false; else add = true; } else{ if (b2r > o2b) add = false; else add = true; } } else{ if (b2r > 0) add = true; else add = false; } if (add) { //+ task.target_pos = ball + Maths::vector2polar(circleR, BallToRobot.angle() + DetAngle); task.orientate = toOppGoalDir; } else { //- task.target_pos = ball + Maths::vector2polar(circleR, BallToRobot.angle() - DetAngle); task.orientate = toOppGoalDir; } break; case shoot: task.target_pos = ball + Maths::vector2polar(5, (ball - opp_goal).angle()); task.orientate = toOppGoalDir; task.needKick = true; task.flag = 1; if (toBallDist < 10 && fabs(model->get_our_player_dir(robot_id) - task.orientate) < 0.15){ task.kickPower = 60; } break;

2023-07-12 上传

const ServerParam & SP = ServerParam::i(); const int self_min = wm.interceptTable()->selfReachCycle(); const int mate_min = wm.interceptTable()->teammateReachCycle(); int opp_min = wm.interceptTable()->opponentReachCycle(); const PlayerObject * opp_fastest = wm.interceptTable()->fastestOpponent(); if ( opp_fastest && opp_fastest->goalie() && wm.gameMode().isPenaltyKickMode() && opp_fastest->pos().dist( wm.ball().pos() ) >= 3.0 ) // MAGIC NUMBER { M_tackle_situation = false; M_opponent_ball = false; dlog.addText( Logger::TEAM, __FILE__":(update) penalty shootouts. not a tackle situation" ); return; } if ( opp_fastest && wm.gameMode().isPenaltyKickMode() && ! opp_fastest->goalie() ) { const AbstractPlayerObject * opponent_goalie = wm.getTheirGoalie(); if ( opponent_goalie ) { /* //yz del std::map< const AbstractPlayerObject*, int >::const_iterator player_map_it = wm.interceptTable()->playerMap().find( opponent_goalie ); if ( player_map_it != wm.interceptTable()->playerMap().end() ) { // considering only opponent goalie in penalty-kick mode opp_min = player_map_it->second; dlog.addText( Logger::TEAM, __FILE__":(update) replaced min_opp with goalie's reach cycle (%d).", opp_min ); } else { opp_min = 1000000; // practically canceling the fastest non-goalie opponent player dlog.addText( Logger::TEAM, __FILE__":%d: (update) set opp_min as 1000000 so as not to consider the fastest opponent.", __LINE__ ); } */ } else { opp_min = 1000000; // practically canceling the fastest non-goalie opponent player dlog.addText( Logger::TEAM, __FILE__":%d (update) set opp_min as 1000000 so as not to consider the fastest opponent.", __LINE__); } }

2023-07-25 上传