Download presentation
Presentation is loading. Please wait.
1
Game Programming II Concept
2
Procedural vs OOP
3
CLASS AND OBJECT
4
WHAT IS CLASS AND OBJECT?
Hold data and function Bundle up package Declare using word “class” Instantiation of “class” Where we use object to call up call as a whole In term of variable, class would be the type while object would be the variable
5
Class Analogy This is class called “SUM” SUM
6
Class Analogy This is class called “SUM” SUM int first; int second;
This is where we put variables/functions inside this class.
7
Class Analogy Class is like a house.
Inside this house we have a lot of stuff “these stuff” is called variables.
8
Class Analogy We also have functions. Example Washing Machine function
Bake a cake function Variables? Cloth, Flour, etc
9
Object? It is like we call a person in the house to do some stuff.
Ahmad Call Samad in the house to wash clothes
10
tambahtolak.h #ifndef tambahtolak_h #define tambahtolak_h class nombor
{ public: int aa,bb,cc,dd; void operasi(nombor dapatkandata); }; #endif
11
tambahtolak.cpp #include <iostream> #include "tambahtolak.h"
using namespace std; void nombor::operasi(nombor dapatkandata) { cout<<"Penambahan a+b : "<<dapatkandata.aa + dapatkandata.bb<<endl; cout<<"Penolakan c-d : "<<dapatkandata.cc - dapatkandata.dd<<endl; }
12
main.cpp #include <iostream> #include <stdlib.h>
#include "tambahtolak.h" using namespace std; nombor datas; nombor *getdatas; int main () { cout<<"Nilai A : ";cin>>datas.aa; cout<<"Nilai B : ";cin>>datas.bb; cout<<"Nilai C : ";cin>>datas.cc; cout<<"Nilai D : ";cin>>datas.dd; getdatas->operasi(datas); system ("pause"); }
13
Hasil
14
Example Program (MyClass.h)
// MyClass.h #ifndef MYCLASS_H #define MYCLASS_H Class MyClass { Private: int x; Public: MyClass(); MyClass(int n); int getx(); }; #endif
15
Example Program (MyClass.cpp)
#include <iostream> #include “MyClass.h” Using namespace std; MyClass::MyClass() { x = 0; } MyClass::MyClass(int n) { x = n; int MyClass::getx(){ return x;
16
Example Program (MainClass.cpp)
#include <iostream> #include “MyClass.h” Using namespace std; void main() { MyClass k[3]; MyClass p[3] = {20, 30, 40}; int i; for(i=0; i<3; i++) { cout<< “k[“ <<i<< “] = “ <<k[i].getx() <<“\t\t”; cout<< “p[“ <<i<< “] = “ <<k[i].getx() << endl; } }
18
class Track {. Float_t fEnergy;. Float_t fMass;. Float_t fMomentum;
class Track { Float_t fEnergy; Float_t fMass; Float_t fMomentum; Track(Float_t mass, Float_t energy); Float_t GetEnergy(); void SetEnergy(Float_t energy); };
19
#include "Track.h" Track::Track(Float_t mass, Float_t energy) { fMass = mass; SetEnergy(energy); } Float_t Track::GetEnergy() { return fEnergy; } void Track::SetEnergy( Float_t energy ) { fEnergy = energy; fMomentum = sqrt( fEnergy*fEnergy - fMass*fMass ); }
20
Accessor
21
Mutators
22
OBJECT ORIENTED DESIGN TECHNIQUE
23
OOD Techniques Encapsulation Polymorphism Inheritance
24
Encapsulation Encapsulation means to design, produce, and describe software so that it can be easily used without knowing the details of how it works. Also known as data hiding
25
Encapsulation An analogy:
When you drive a car, you don’t have know the details of how many cylinders the engine has or how the gasoline and air are mixed and ignited. Instead you only have to know how to use the controls.
26
Encapsulation (Advantages)
Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Also, an object can be easily passed around in the system. Eg: You can give your bicycle to someone else, and it will still work. Information hiding: An object has a public interface that other objects can use to communicate with it. The object can maintain private information and methods that can be changed at any time without affecting the other objects that depend on it. You don't need to understand the gear mechanism on your bike to use it.
27
ii) Polymorphism The same word or phrase can be mean different things in different contexts The same behavior maybe exhibited differently on different class or subclasses (object). Analogy: in English, bank can mean side of a river or a place to put money
28
iii) Inheritance A way of organizing classes
Term comes from inheritance of traits like eye color, hair color, and so on. Classes with properties in common can be grouped so that their common properties are only defined once To design two or more entities that are different but share many common features.
29
Inheritance Vehicle Automobile Motorcycle Bus Sedan Sports Car
Luxury Bus School Bus
30
Inheritance First we define a class that contains the common features of the entities. Then we define classes as an extension of the common class inheriting everything from the common class. We call the common class the parentclass and all classes that inherit from it childclasses.
31
Inheritance
32
Inheritance (Continue)
Explanation: The class Manager has all the attributes and operations of the class Employee but has, in addition 2 new attributes i) that record the budget controlled by the manager ii) the date that the manager was appointed to a particular management role.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.