1 / 17

Practical test tomorrow

Practical test tomorrow. Will involve writing a simple class instantiating objects other C++ constructs as practised in the lab sheets to date inheritance and or aggregation. Getting output onto the screen. include the <iostream.h> header file use cout <<

tevin
Download Presentation

Practical test tomorrow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practical test tomorrow • Will involve writing a simple class • instantiating objects • other C++ constructs as practised in the lab sheets to date • inheritance and or aggregation

  2. Getting output onto the screen • include the <iostream.h> header file • use cout << • use additional << to combine multiple elements together eg cout << “you entered “ << x; • use endl to add newline cout << “you entered” << x << endl;

  3. Getting input from the keyboard • include the <iostream.h> header file • declare a variable of a suitable type eg int num_in; • use cin eg cin >> num_in;

  4. Getting input from the keyboard #include "stdafx.h" #include <iostream.h> int main( ) { int x; cout << "please enter an integer" << endl; cin >> x; cout << "you entered " << x << endl; return 0; }

  5. Using a switch statement • What variable are you going to switch on? • What cases will you respond to? • Can provide a default case

  6. Using a switch statement • Type the skeleton of the switch statement first and compile • then flesh it out with cases • remember the break statement at the end of each case

  7. Using a switch statement switch(x) { case 1: //statements here ... break; case 2: //statements here ... break; default: //statements here ... break; }

  8. Using the switch statement switch(x) { case 1: cout << "you entered 1" << endl; break; case 2: cout << "you entered 2" << endl; break; default: cout << "you didn't enter 1 or 2" << endl; break; }

  9. Writing classes • Remember the notion of data hiding and encapsulation • attributes are usually declared private • public set and get methods provided to control access to the attributes • class classname • { • private: • public: • };

  10. Writing classes class classname { private: int x; public: void setX(int x_in); int getX(void); };

  11. Writing classes • Methods can either be written inline in the class definition... class classname { private: int x; public: void setX(int x_in){x=x_in;} int getX(void){return x;} };

  12. Writing classes • …or for longer methods, a separate method implementation is usually added after the main() function, using the :: scope resolution operator class classname { private: int x; public: void setX(int x_in); int getX(void); }; void classname::setX(int x_in) { x=x_in; } int classname::getX() { return x; }

  13. Writing classes class exam { private: int numQuestions; public: void setNumQuestions(int num_in); int getNumQuestions(); }; void exam::setNumQuestions(int num_in) { numQuestions=num_in; } int exam::getNumQuestions() { return numQuestions; }

  14. Writing functions int main() { exam myExam; myExam.setNumQuestions(5); cout << "there are " << myExam.getNumQuestions() << "questions in the exam" << endl; return 0; }

  15. Inheritance • A class can inherit from another class if the ‘a kind of’ relationship is appropriate - eg a dog is a kind of animal. • Use the colon : operator to indicate the base class when declaring your derived class class dog: public animal { … };

  16. Good Luck for today • Don’t panic • Be methodical - read the question carefully and plan your approach on paper • add the minimum framework for your (function, class, method, for-loop etc) and compile it before ‘fleshing it out’. If you run out of time, just leave the empty shell.

  17. Good Luck for today • Name variables and functions sensibly • Use whitespace to make code easy to read • Comment where useful but don’t comment things which are obvious

More Related