Object-Oriented Programming (OOP)

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is a primary benefit of using Object-Oriented Programming (OOP)?

  • It reduces memory usage by compressing code into smaller files.
  • It automatically fixes bugs and vulnerabilities in the code.
  • It makes code easier to manage, change, and update due to its modular structure. (correct)
  • It increases code execution speed by optimizing algorithms.

Which of the following best describes the concept of a 'class' in OOP?

  • A pre-built function in a programming language.
  • A specific instance of an object with defined properties.
  • A blueprint for creating objects, defining their properties and methods. (correct)
  • A section of code that performs a calculation.

What does 'encapsulation' achieve in object-oriented programming?

  • It protects the internal workings of code by controlling access to variables and functions. (correct)
  • It allows objects of different classes to inherit properties from each other.
  • It ensures that all variables are globally accessible to all programs.
  • It automatically converts code from one programming language to another.

How does proper encapsulation benefit software development?

<p>It ensures that changes to a class do not affect other parts of the program, provided the exposed parts are still accessed in the same way. (A)</p> Signup and view all the answers

What does 'polymorphism' enable in programming?

<p>The ability to write code that can work with objects of different types more generically. (D)</p> Signup and view all the answers

In the context of OOP, what is the primary advantage of using inheritance?

<p>It enables a class to acquire properties and behaviors from another class, promoting code reuse. (B)</p> Signup and view all the answers

What is the significance of 'design patterns' in software development?

<p>They provide proven solutions to common problems in software design, improving code structure and maintainability. (C)</p> Signup and view all the answers

In C++, what is the purpose of declaring a class?

<p>To define new data types that can contain functions, variables, and loops. (D)</p> Signup and view all the answers

How are class functions typically defined in relation to the class declaration in C++?

<p>They are defined in a separate <code>.cpp</code> file with the same name as the class. (B)</p> Signup and view all the answers

What is the role of the private: keyword in a C++ class?

<p>It restricts access to variables and functions only to the class itself. (B)</p> Signup and view all the answers

What part of the class declaration does the public: keyword identify?

<p>Functions that are accessible from outside the class. (D)</p> Signup and view all the answers

In C++, what is a 'member variable' and why is the m_ prefix used?

<p>A variable declared as part of a class; <code>m_</code> indicates it's a member of the class. (A)</p> Signup and view all the answers

What is the purpose of a 'scope resolution operator' :: in C++?

<p>To specify which class a function belongs to when defining the function outside the class declaration. (B)</p> Signup and view all the answers

In C++, what is an 'instance' of a class?

<p>A particular object created from a class blueprint. (B)</p> Signup and view all the answers

When is a class constructor function called in C++?

<p>When an object of the class is declared. (C)</p> Signup and view all the answers

What is a characteristic of a constructor function in C++?

<p>It has the same name as the class. (C)</p> Signup and view all the answers

What is the function of FloatRect getPosition() within a class?

<p>To return the positional coordinates of the four corners defining the object. (A)</p> Signup and view all the answers

What is the purpose of a 'getter function' in C++?

<p>To return the value of a private member variable. (B)</p> Signup and view all the answers

Why is the update function located within the Bat or Ball class rather than in the main function?

<p>To encapsulate movement logic within the class, adhering to OOP principles. (D)</p> Signup and view all the answers

In the Pong game project, what does the Bat::update(Time dt) function do?

<p>It updates the Bat's position based on player input and the elapsed time. (B)</p> Signup and view all the answers

In the Pong game context, what is the role of the #pragma once directive?

<p>It prevents a header file from being included more than once in a single compilation unit. (B)</p> Signup and view all the answers

What does the expression dt.asSeconds() represent in the context of the game's update logic?

<p>The time elapsed since the last frame in seconds. (D)</p> Signup and view all the answers

What is the purpose of the getKeyPressed function in the main loop?

<p>To detect if a key has been pressed by the user. (A)</p> Signup and view all the answers

When implementing collision detection in the Pong game, what does the intersects function do?

<p>It returns whether two FloatRect objects overlap. (C)</p> Signup and view all the answers

In the Pong game, which of the following is the purpose of the reboundSides() function?

<p>To reverse the horizontal direction of the ball when it hits either side of the screen. (A)</p> Signup and view all the answers

In the Pong game, what happens when the ball hits the bottom of the screen?

<p>The ball is repositioned at the top-center of the screen and a life is lost. (D)</p> Signup and view all the answers

In the Pong game, what is the purpose of SFML/Graphics.hpp?

<p>It contains all the SFML classes that allow the drawing of shapes, sprites, and text to the screen. (C)</p> Signup and view all the answers

In the Ball class, what purpose does the m_DirectionX variable serve?

<p>It determines the direction and speed the Ball moves horizontally. (A)</p> Signup and view all the answers

For the Bat class created for the Pong game, which statement best describes the relationship between the header file and source file?

<p>The header file declares the class and contains the definitions of the functions that are used and the source file contains all class function implementation. (D)</p> Signup and view all the answers

In the code void Bat::moveRight(), what does Bat:: indicate?

<p>It references that the <code>moveRight()</code> function belongs to the <code>Bat</code> class. (A)</p> Signup and view all the answers

When creating the Pong project, what is the final step?

<p>Drawing the objects to the screen. (B)</p> Signup and view all the answers

After creating the Bat class and compiling the program, what needs to take place inside the main function to use this Bat object?

<p>An instance should be created, like so: <code>Bat bat;</code> (B)</p> Signup and view all the answers

In terms of increasing or decreasing difficulty, what is one way to modify the code to increase Ball speed?

<p>Increase the speed by multiplying the value of <code>m_Speed</code>. (B)</p> Signup and view all the answers

When adding collision detection using code such as: if (ball.getPosition().top < 0), what does ball.getPosition().top refer to?

<p>It gets the pixel coordinate from the top of the ball to determine if it has collided. (D)</p> Signup and view all the answers

When creating classes for the Pong game and naming different objects and methods, why would the name moveLeft be preferred over ml?

<p>The name <code>moveLeft</code> is more descriptive and it aids in understanding the purpose of the method. (A)</p> Signup and view all the answers

When the pong ball makes contact with an object or the top/sides of the window, what code assists in making the ball appear to reflect?

<p><code>ball.reboundBatOrTop()</code> or <code>ball.reboundSides();</code> (A)</p> Signup and view all the answers

What functions are necessary to be included in the final main.cpp file?

<p>The functions that define the text, the color, and the fonts. (B)</p> Signup and view all the answers

Flashcards

Object-Oriented Programming

A programming style decomposing tasks into reusable components (objects) with data and behavior.

Class

A template or blueprint for creating objects, defining data and behavior.

Object (of a class)

A specific realization or instance of a class.

Encapsulation

Protecting internal code from outside interference through access control.

Signup and view all the flashcards

Polymorphism

Writing code that operates on different types in a general and efficient way.

Signup and view all the flashcards

Inheritance

Adopting features and benefits from existing classes, reusing and refining code.

Signup and view all the flashcards

Design Patterns

A structured way to organize code for readability and maintainability.

Signup and view all the flashcards

Member Variables

Variables that hold data within a class.

Signup and view all the flashcards

Constructor function

A special function to create an object prepare it for use.

Signup and view all the flashcards

Getter function

Function that pass back private data from a class.

Signup and view all the flashcards

reboundSides()

Reverses direction on collision with sides.

Signup and view all the flashcards

reboundBatOrTop()

Reverses direction for bat or top collision.

Signup and view all the flashcards

reboundBottom()

Repositions ball after bottom is hit.

Signup and view all the flashcards

Study Notes

  • This chapter introduces Object-Oriented Programming (OOP)
  • The theory provides knowledge necessary to use OOP with expertise.
  • This OOP theory provides the basis for coding an actual Pong game project.
  • The chapter covers OOP and classes using a hypothetical Bat class and start working on the Pong game and code a real class to represent the player's bat.

OOP Paradigm

  • OOP is a programming paradigm considered to be the standard way to code
  • There are non-OOP ways to code, including non-OOP game coding languages/libraries
  • Starting from scratch, there isn't any reason not to use OOP

Benefits of OOP

  • Enables easier code management, changes, and updates
  • Facilitates quicker and more reliable code writing
  • Allows easy utilization of other people's code like SFML

Core Concept of OOP

  • OOP programming involves breaking down requirements into manageable chunks called objects
  • Objects are self-contained and reusable by other programs
  • Classes serve as blueprints for objects.

Class Implementation

  • Implementing an object of a class creates an instance of the class
  • Classes in games represent real-world things
  • Classes can be written for a bat controlled by the player or a ball bounced around the screen
  • The core principles of OOP are encapsulation, polymorphism, and inheritance

Encapsulation Defined

  • Encapsulation keeps the internal workings of code safe from outside interference
  • Achieved by controlling access to variables and functions
  • Enables code updates and improvements without affecting programs that use it

Polymorphism Defined

  • This allows writing code less dependent on the types being manipulated
  • Results in clearer and more efficient code
  • "Polymorphism" means that objects can take on different forms

Inheritance Defined

  • This allows harnessing features and benefits of other people's classes
  • Includes encapsulation and polymorphism
  • It refines code for specific situations

Advantages of Using OOP

  • Facilitates adding new features without disrupting existing ones
  • Encapsulation minimizes consequences when changing a class

Code Reusability

  • Code can be used from other's without needing to know how it works inside
  • Complicated concepts like multiplayer and directional sound can be used

Class Variations

  • Similar, different versions of a class can be created using inheritance
  • Original functions can be used for a new object

Planning and Design

  • Planning and design are vital to successful OOP and game development,
  • Well-structured code matters more than just knowing C++, SFML, and OOP topics
  • Structuring code is called design patterns
  • Effective use of design patterns becomes more important as code gets complex
  • Design patterns can be learned as the complexity of projects grows

Class Defined

  • A class is a collection of code containing functions, variables, and loops
  • Each new class is declared in its own .h file with the same name
  • Functions of the class are defined in its own .cpp file
  • A class can be used to create multiple objects, with the class being the blueprint

Class Code Example

  • The class keyword declares a new class in curly braces, followed by a semicolon
  • Variables are declared
  • These are prefixed with m_ as a convention
  • Variables declared as part of a class are called member variables
  • Private variables are not directly accessible by users
  • Good encapsulation makes variables private whenever possible

Variables

  • The public section solves the problem of private variables needing to be accessed
  • Provides public functions to work with objects of the specified type
  • In summary, there is a bunch of inaccessible (private) variables that cannot be used from the main function

Class Functions

  • Class functions are in a separate file with the same name as the class and the .cpp extension
  • Include directives are used to include class and function declarations from the .h file
  • The scope resolution operator :: is used because the functions connect to a class
  • The function name is prefixed with the class name, as well as ::

Instances

  • An instance of the Bat class can be created:
    • Bat bat;
  • Public functions can be accessed
  • This can be used for code for multiplayer functionality
  • Each instance of Bat has its own set of variables

Create Project

  • To create a project, follow this:
  • Start Visual Studio and click Create New Project button. If the Timber project is open, select File | New project.
  • In the window, choose Console app and click Next button. The Configure your new project window will display.
  • In the Configure your new project window, type Pong in the Project name field.
  • Visual Studio configures the Solution name field automatically.
  • In the Location field, browse to the VS Projects folder, where all project files are stored.
  • Select the project properties by selecting Project | Pong properties from the main menu.

Additional configurations

  • In the Pong Property Pages window, select All Configurations from the Configuration drop-down
  • Select C/C++ and General from the left-hand menu.
  • Locate the Additional Include Directories edit box and type the drive letter where the SFML folder is located, followed by \SFML\include.
  • Select Linker, then General.
  • Find the Additional Library Directories edit box, and type the SFML folder, followed by \SFML\lib.

Final Steps

  • Select Linker and Input.
  • Copy and paste/type sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-network-d.lib;sfml-audio-d.lib; into the Additional Dependencies edit box. Position the cursor carefully at the start.
  • Click OK and then Apply.
  • Copy the SFML.dll files into the main project directory from the SFML\bin folder.
  • Create a new folder called fonts in the VS Projects\Pong folder to house the Heads Up Display (HUD).
  • Add the DS-DIGIT.ttf file into the VS Projects\Pong\fonts folder.

Bat Class Intro

  • Classes can be simple like the preceding Bat class, or contain other objects from other classes
  • When making games, several things are missing from the hypothetical Bat class, for example drawing the Bat so it needs a sprite, and sometimes a texture

Bat.h Code

  • We need a way to control the rate of animation for all game objects
  • The Header File (.h) must be created
  • #pragma once prevents processing the file more than once, speeding compilation

File Contents

  • Vector2f is called m_Position, which holds the horizontal and vertical position of the player's bat
  • SFML RectangleShape makes the bat appear on the screen
  • Boolean: track direction
  • Float: pixels per second when moving left/right
  • Bat function is the constructor, meaning its the same name as its class

Functions

  • getPosition: returns FloatRect (four points of a rectangle)
  • getShape: returns RectangleShape
  • Other Functions: for Bat movements.
  • moveLeft/Right: controls if, when, and which way the bat moves.
  • update: Calculates how to move the bat each frame.
  • Calls an update function to the Bat instance and passes in the change in time.
  • The Bat class gets the change in time and moves the bat based on the directions, the player moves them, and the desired speed of the bat.

Functions

  • Classes: contains source code and is often split into a few parts, such as the member variable and function declarations
  • Code: will detect if the player holds down the left arrow key; if so, the moveLeft will be called
  • Main: handles the player quitting the game by pressing the Escape key
  • Update: Bat object in our main game loop, or our draw function to call the getShape function
  • Draw: Uses the rectangle shape from our current Bat object to draw to the screen

Summary of process

  • New file created and saved as Bat.cpp
  • Added constructors to Bat method
  • In order to get the graphics, you use SFML libraries.
  • When determining that positions are called we will get the positions of the graphics in the main loop

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser