320 likes | 424 Views
Concepts in C++. Games Audio Programming. Lecture 1. About the module Lectures/Tutorials The Assessment Assignment Language introduction. Module Text. Deitel, H.M. & Deitel, P.J. (2003) C++ How to program , Prentice Hall, U.S. ISBN 0-13-111881-1
E N D
Concepts in C++ Games Audio Programming
Lecture 1 • About the module Lectures/Tutorials • The Assessment Assignment • Language introduction
Module Text • Deitel, H.M. & Deitel, P.J. (2003) C++ How to program, Prentice Hall, U.S. ISBN 0-13-111881-1 • Liberty, J. & Horvath, D.B. (2005) Teach Yourself C++ in 24 Hours,Sams, USA. ISBN 0-672-32681-7 • Overland, B. (2005)C++ Without FearPrenctice Hall, USA. ISBN 0-321-24695-0
Module Text for C++ • If available – second hand?Graham, N (1991) Learning C++McGraw Hill, USA. ISBN 0-07-100849-7 • Eckle, B. (2000) Thinking in C++ (Volume 1)Eckle, B. (2003) Thinking in C++ (Volume 2) • Both available as free downloadhttp://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html • Vol 1 – 867KB; Vol 2 – 991KB
Module Schedule • Availability • Blackboard • Available beyond University Network
Integrated Development Environment (IDE) • Using Microsoft Visual Studio.NET • Strictly C++ • Initial User Guide on Blackboard • Self discovery also required • Other IDE may be used – Borland, UNIX/Linux, Command Line) as long as strictly C++ • Should run on University computers though may use Notepads / Laptops.
Class – Specifying the access • Class – default will not allow access • Access Specifiers • private: default • public: allows external access • protected: restricted external access • Concentrate on public & private for now. • protected covered in later lecture
Constructor usage • Initialisation parameters added to code • e.g.int main(void){ Account Tom(250.00, 5.3);} • Tom’s account will be instantiated with… • An opening balance of £250.00 and • An opening interest rate of 5.3%
Using a prototype #include <iostream> using namespace std; void function1(bool val); void function2(bool val); int main() { function1(true); function2(true); } void function2(bool val) { cout << "This is function 2" << endl; if (val) function1(!val); } void function1(bool val) { cout << "This is function 1" << endl; if (val) function2(!val); } function prototypes or declarations function definitions
Header files • when developing larger programs, it is useful to put the prototypes in a separate header file • suffix .h, for example MyFunctions.h • any program file that uses these functions can include the header file #include “MyFunctions.h” • this enables the compiler to check the parameters and return type of any functions called are correct • without needing to access the function definitions • header files can also contain constants, enums, structures and class declarations • the use of header files allows the same declarations to be used in many .cpp files
Guarding against multiple inclusions • functions can be declared multiple times in the same source file • prototypes repeated • but defined only once • method body • structures, enums and classes can be declared only once • it's easy to include the header file twice in the same source file • guard against this by using conditional definition or pragma
Account.h #ifndef ACCOUNT_H #define ACCOUNT_H class Account { private: double balance; double interestRate; public: Account(double initialBalance, double rate); double getBalance(); void deposit(double amount); void withdraw(double amount); void addInterest(); }; #endif
account.cpp #include "account.h" Account::Account(double initialBalance, double rate) : balance(initialBalance), interestRate(rate) { } double Account::getBalance(){return balance;} void Account::deposit(double amount) { balance += amount; } void Account::withdraw(double amount) { // implement this method yourself } void Account::addInterest() { balance *= (1 + interestRate/100.0); }