PRG 2781
Classes and Objects
www.belgiumcampus.ac.za
Learning Objectives
• Understanding C# Programming
• Defining a class and an object
• Defining Class members
• Creating Classes and Objects
2
www.belgiumcampus.ac.za
1.1 Background
Introduction
C# is a modern, high-level, general-purpose, object-oriented programming
language.
• Principal language of the .NET framework.
• A framework is a platform for developing software applications.
• Includes predefined classes and functions that can be used to process input,
input, manage hardware devices, and interact with system software
• C# is used to create applications for stand alone, web based and mobile APIs.
• It is a one single programming language for multiple device range.
3
www.belgiumcampus.ac.za
1.2 Key Terms
In this slide, will discuss some of the key concepts and their practical
implementation in C#.
These include:
• Classes
• Objects
• Methods
4
www.belgiumcampus.ac.za
Classes and Objects
Example#1: Consider an ATM.
Think of it as a class.
It’s a machine which is pretty much useless until
you insert your card [an object].
After you insert your card, the machine gets
information, from your card, about you and your
bank account and the balance in it [information
about the object].
5
www.belgiumcampus.ac.za
Example#1: Consider an ATM…
Once you use a class (ATM) and its object (card), now you can perform
operations on the object, like withdrawal of money or checking you
balance or getting statement of your account.
These operations will be methods belonging to that class (ATM) [but you
cannot use them until you create an object out of it.]
Methods (operations) belong to a class (ATM), and we use objects (card)
access to those methods.
6
www.belgiumcampus.ac.za
Defining a Class
A class is a user-defined blueprint or prototype from which objects are created.
7
www.belgiumcampus.ac.za
Example#2: Consider Automobiles
An automobile is a means of transportation that usually has wheels and an
engine.
Automobile - example of class
Car, Bus, Bike (e.gs of automobiles) - possible sub-classes of automobile
class
Polo, i20 - Objects of Car class
Quantum, Minibus – Objects of Bus class
Scooter, Cruiser – Objects of Bike class
8
www.belgiumcampus.ac.za
Defining Objects
An object is an instance of a
created class.
Each object will have a state and some behavior.
An object's state is the data or information it contains. Behavior in an object is often
represented by methods.
9
www.belgiumcampus.ac.za
Defining Behavior in a Class
A Person with name = Alfred, height =
1,72m, weight = 95kgs, gender = male,
and age = 43 can walk, run, jump, speak
and sleep.
Person is the class, which can define
ANYONE else (say Nicole), with different
attributes, but has same behaviors
10
www.belgiumcampus.ac.za
Classes and Objects
11
www.belgiumcampus.ac.za
Creating a Class
Classes are declared by using the keyword class followed by the class name and a
set of class members surrounded by curly braces { }.
A Class is made up of the following:
Access modifiers - specify access rules for the
members and the class itself. [private, public or
internal]
class keyword and class name (Example)
Class Members - specifies properties, data fields and
methods (functions) that operate on the data in the
variables.
12
www.belgiumcampus.ac.za
Class Members
• Classes in C# can have members:
• Data fields, methods, properties, indexers, events, operators,
constructors, destructors, …
• Members can have access modifiers
• public, private, …
• Members can be
• static (common) or specific for a given object
13
www.belgiumcampus.ac.za
Fields and
Methods
Fields:
A field (variable), in C#, is a member of a class or an object of any type that
represents a memory location for storing a value.
E.g int myAge;
Methods:
A method is a group of statements that together perform a task. Every C#
program has at least one class with a method named Main.
To use a method, you need to:
• Define the method
• Call/Invoke the method
14
www.belgiumcampus.ac.za
Creating a Class in Visual Studio
2017/2019/2022
Example: Namespaces (collection of predefined
classes/elements)
You create your own Namespace (which
you define)
You can create your own class, with your
own methods
Default class which runs your
program, with Main method
15
www.belgiumcampus.ac.za
Basic syntax for class declaration
In C#, individual classes and class members can be prefixed with access
modifiers.
By default, if you declare a member variable (or anything else) in a class but
do not specify its access level, the member is considered private and
cannot be accessed from outside, i.e. by a non-member of that class.
Therefore, to make a member accessible by other classes, you must declare
it as public.
You can use a mix of public and private members in a class and there is no
rule on which access level should be listed first or last.
16
www.belgiumcampus.ac.za
Creating a class object
We use the new operator when creating an instance of the class
(object).
Custom class House
Object beachHouse, of type House
17
www.belgiumcampus.ac.za
Class and Object Example:
Question:
Create a custom class with member field myAge, and a method
printMyAge() that will display the myAge field. Create an object
of the class, that will invoke the printMyAge() method, in main
method.
18
www.belgiumcampus.ac.za
Class and Object Example:
Solution:
19
www.belgiumcampus.ac.za
Class and Object Example:
Solution:
20
www.belgiumcampus.ac.za
Exercise
1. Create a Box class with length and width field members, which will
utilize a calculateArea() method member to print the area of the box. Use
literal values for your fields.
2. Using classes, objects, fields, methods and console input and output,
create a console App that will run as follows:
21
www.belgiumcampus.ac.za
THANK YOU
/belgiumcampusSA
#Belgium Campus
22
www.belgiumcampus.ac.za