Difference between revisions of "Section C 2022"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Question Help)
(Virtual & Override)
Line 66: Line 66:
  
 
Private means it is only available from within an object or the class itself. Protected is relatively the same as private, except it will also be available in any subclass.
 
Private means it is only available from within an object or the class itself. Protected is relatively the same as private, except it will also be available in any subclass.
 +
 +
==Inheritance & Polymorphism==
 +
Remember a class can be used as the basis for subclass, when this happens all of the data values and methods of the base class will automatically be available in the subclass. This is called 'Inheritance'. So in the skeleton program the Card class as been used to create the subclasses of DifficultyCard & ToolCard. Both DifficultyCard & ToolCard will inherit everything not declared as 'Private', you therefore only need to declare what is new within a subclass.
 +
 +
Polymorphism is the idea that what a subclass inherits can be replaced with the subclasses own version. A subclass can therefore override a method which it inherits from its parent or base class.
  
 
==Virtual & Override==
 
==Virtual & Override==
 +
These terms are related to Polymorphism above.
 +
 
Any method declared as 'virtual' can be overridden in a subclass. So some methods within the skeleton program are already set to be 'virtual', for example the Card class as 4 methods declared as 'virtual'. Then in ToolCard 'GetDescription' is overridden and in DifficultyCard both 'GetDescription' & 'Process' are overridden.
 
Any method declared as 'virtual' can be overridden in a subclass. So some methods within the skeleton program are already set to be 'virtual', for example the Card class as 4 methods declared as 'virtual'. Then in ToolCard 'GetDescription' is overridden and in DifficultyCard both 'GetDescription' & 'Process' are overridden.
  
* About 'Inheritance' & Polymorphism'
 
 
* About the use of 'this'
 
* About the use of 'this'
 
* About the use of 'base'
 
* About the use of 'base'

Revision as of 10:27, 24 December 2021

This section is to document the skeleton program. Section C will be questions relating to the actual code within the program and could relate to specific methods, variables, programming skills, or theory topics.

Execution

The 'static void Main' in the class of program will always be the first block of code executed.

The line:

Breakthrough ThisGame = new Breakthrough();

Declares and instantiates an object of 'Breakthrough', this will cause the constructor method of the 'Breakthrough' class to be run. The code for the constructor is below:

        public Breakthrough()
        {
            Deck = new CardCollection("DECK");
            Hand = new CardCollection("HAND");
            Sequence = new CardCollection("SEQUENCE");
            Discard = new CardCollection("DISCARD");
            Score = 0;
            LoadLocks();
        }

The 'CardCollection' objects ('Deck','Hand','Sequence' & 'Discard') are already declared, but the use of 'new CardCollection' will instantiate them and will cause the constructor method of 'CardCollection' to run for each object. The constructor method for the 'CardCollection' class is:

        public CardCollection(string n)
        {
            Name = n;
        }

The final line of the 'Breakthrough' constructor calls the 'LoadLocks' method of the 'Breakthrough' class. The 'LoadLocks' method will execute and doesn't make any other called to other methods.

So back to the 'static void Main' and this line:

ThisGame.PlayGame();

This calls the 'PlayGame' method of the 'Breakthrough' object.

Classes

The skeleton program has the following classes:

2022 - Breakthrough

2022 - Challenge

2022 - Lock

2022 - Card

2022 - ToolCard

2022 - DifficultyCard

2022 - CardCollection

2022 - Program

Question Help

Private vs Protected

Remember a variable or method can be declared Public, Private, Protected. The default for C# is private, so any method or variable which doesn't specifically declare Public or Protected is automatically Private. But what does this really mean, well a Public variable or method is accessible from outside of the object. So in terms of the skeleton program, if you create a Card object called 'temp' then you will be able to access 'temp.GetCardNumber()'. However the actual variable CardNumber is protected so you can't access it from outside and you can't therefore access 'temp.CardNumber'.

Private means it is only available from within an object or the class itself. Protected is relatively the same as private, except it will also be available in any subclass.

Inheritance & Polymorphism

Remember a class can be used as the basis for subclass, when this happens all of the data values and methods of the base class will automatically be available in the subclass. This is called 'Inheritance'. So in the skeleton program the Card class as been used to create the subclasses of DifficultyCard & ToolCard. Both DifficultyCard & ToolCard will inherit everything not declared as 'Private', you therefore only need to declare what is new within a subclass.

Polymorphism is the idea that what a subclass inherits can be replaced with the subclasses own version. A subclass can therefore override a method which it inherits from its parent or base class.

Virtual & Override

These terms are related to Polymorphism above.

Any method declared as 'virtual' can be overridden in a subclass. So some methods within the skeleton program are already set to be 'virtual', for example the Card class as 4 methods declared as 'virtual'. Then in ToolCard 'GetDescription' is overridden and in DifficultyCard both 'GetDescription' & 'Process' are overridden.

  • About the use of 'this'
  • About the use of 'base'
  • Constructors
  • Static
  • Lists