Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
classes
Classes (Object-Oriented Programming)
What is a class?
-
A class is a blueprint used to create objects in Object-Oriented Programming (OOP)
-
It defines a structure for attributes (data) and methods (behaviour)
Key terms
|
Term |
Definition |
|---|---|
|
Object |
An instance of a class with its own state and behaviour |
|
Attribute |
A variable defined in a class (also called a class variable) |
|
Instance Variable |
Attribute stored inside an individual object |
|
Method |
A function defined within a class; describes object behaviour |
|
Instantiation |
The process of creating an object from a class |
|
Identifier |
The name used to refer to an object |
Example
-
A
Studentclass could contain:-
Attributes:
name,dateOfBirth,gender -
Each
Studentobject (e.g.John,06/10/2015,Male) has its own values
-
Prebuilt vs custom classes
|
Prebuilt classes |
Custom classes |
|---|---|
|
Provided by the language |
Defined by the programmer |
|
E.g. |
E.g. |
-
Below is a visual representation of both a class and objects that have been instantiated
-
In the image, the identifiers are P1 and P2

Example of a class and objects
Programming classes (OOP)
How do you program a class?
-
In this example we will:
-
Define a class for a car sales program where the following is required:
-
Name of Manufacturer
-
Model of Car
-
Price
-
Mileage
-
Whether the car is preowned
-
-
-
Instantiate two objects for the car sales program ensuring that they have appropriate identifiers
-
List two methods that would be useful for a car object
Pseudocode

Pseudocode for creating classes, a constructor, and functions
Java
//creating a class called cars public class Cars { //creating class attributes for a car private String Manufacturer; private String Model; private int Price; private int Mileage; private boolean PreOwned; //Constructor - This is used to create the objects. More on this in the object chapter) public Cars(String manufacturer, String model, int price, int mileage, boolean preOwned) { this.Manufacturer = manufacturer; this.Model = model; this.Price = price; this.Mileage = mileage; this.PreOwned = preOwned; } //method to make the car go faster. public void Accelerate(){ //code to be executed which will make the car go faster } //Method to honk horn public void honkHorn(){ // code to be executed which will honk the car horn } }
Python
#creating a class called cars class Cars: # constructor - This is used to create the objects. More on this in the object chapter) def __init__(self, manufacturer, model, price, mileage, preOwned): self.Manufacturer = manufacturer self.Model = model self.Price = price self.Mileage = mileage self.PreOwned = preOwned #method to make the car go faster. def Accelerate(self): # code to be executed which will make the car go faster # method to honk horn def honkHorn(self): # code to be executed which will honk the car horn
Worked Example
A computer game is being designed that will include different vehicles. A prototype for the game is being developed using object‑oriented programming.
The class Vehicle stores data about the vehicles. Each vehicle has an identification name, a maximum speed, a current speed and a horizontal position. The value IncreaseAmount is added to the current speed each time the vehicle increases its speed.

Write program code to declare the class Vehicle. All attributes must be private.
You only need to declare the class and its constructor. Do not declare any other methods.
Use your programming language’s appropriate constructor.
If you are writing program code in Python, include attribute declarations using comments. [5]
Answer
-
Class header (and close where appropriate) [1 mark]
-
5 (private) attribute declarations including data types [1 mark]
-
Constructor header (and close where appropriate) taking 3 parameters (min) [1 mark]
-
Assigning
ID, MaxSpeedandIncreaseAmountto parameters [1 mark] -
Assigning
CurrentSpeedandHorizontalPositionto 0 [1 mark]
Example program code:
VB.NET
Class Vehicle Private ID As String Private MaxSpeed As Integer Private CurrentSpeed As Integer Private IncreaseAmount As Integer Private HorizontalPosition As Integer Sub New(IDP, MaxSpeedP, IncreaseAmountP) ID = IDP MaxSpeed = MaxSpeedP CurrentSpeed = 0 IncreaseAmount = IncreaseAmountP HorizontalPosition = 0 End Sub
End Class
Java
class Vehicle{ private String ID; private Integer MaxSpeed; private Integer CurrentSpeed; private Integer IncreaseAmount; private Integer HorizontalPosition; public Vehicle(String IDP, Integer MaxSpeedP, Integer IncreaseAmountP { ID = IDP; MaxSpeed = MaxSpeedP; IncreaseAmount = IncreaseAmountP; CurrentSpeed = 0; HorizontalPosition = 0; }}
Python
class Vehicle: #self.__ID string #self.__MaxSpeed integer #self.__CurrentSpeed integer #self.__IncreaseAmount integer #self.__HorizontalPosition def __init__(self, IDP, MaxSpeedP, IncreaseAmountP): self.__ID = IDP self.__MaxSpeed = MaxSpeedP self.__IncreaseAmount = IncreaseAmountP self.__CurrentSpeed = 0 self.__HorizontalPosition = 0
Responses