170 likes | 330 Views
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 <<
E N D
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 << • use additional << to combine multiple elements together eg cout << “you entered “ << x; • use endl to add newline cout << “you entered” << x << endl;
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;
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; }
Using a switch statement • What variable are you going to switch on? • What cases will you respond to? • Can provide a default case
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
Using a switch statement switch(x) { case 1: //statements here ... break; case 2: //statements here ... break; default: //statements here ... break; }
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; }
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: • };
Writing classes class classname { private: int x; public: void setX(int x_in); int getX(void); };
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;} };
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; }
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; }
Writing functions int main() { exam myExam; myExam.setNumQuestions(5); cout << "there are " << myExam.getNumQuestions() << "questions in the exam" << endl; return 0; }
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 { … };
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.
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