Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance.

Similar presentations


Presentation on theme: "1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance."— Presentation transcript:

1 1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance and or aggregation

2 2 Getting output onto the screen yinclude the header file yuse cout << yuse additional << to combine multiple elements together eg cout << “you entered “ << x; yuse endl to add newline cout << “you entered” << x << endl;

3 3 Getting input from the keyboard yinclude the header file ydeclare a variable of a suitable type eg int num_in; yuse cin eg cin >> num_in;

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

5 5 Using a switch statement zWhat variable are you going to switch on? zWhat cases will you respond to? zCan provide a default case

6 6 Using a switch statement zType the skeleton of the switch statement first and compile zthen flesh it out with cases zremember the break statement at the end of each case

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

8 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 9 Writing classes yRemember the notion of data hiding and encapsulation yattributes are usually declared private ypublic set and get methods provided to control access to the attributes yclass classname y{ yprivate: ypublic: y};

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

11 11 Writing classes zMethods 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 12 Writing classes z…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 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 14 Writing functions int main() { exam myExam; myExam.setNumQuestions(5); cout << "there are " << myExam.getNumQuestions() << "questions in the exam" << endl; return 0; }

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

16 16 Good Luck for today zDon’t panic zBe methodical - read the question carefully and plan your approach on paper zadd 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 17 Good Luck for today zName variables and functions sensibly zUse whitespace to make code easy to read zComment where useful but don’t comment things which are obvious


Download ppt "1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance."

Similar presentations


Ads by Google