Injustice - Developers Among Us (SciFiDevCon 2024)
07 iec t1_s1_oo_ps_session_10
1. Object-Oriented Programming Using C#
Objectives
In this session, you will learn to:
Implement operator overloading
Identify relationships between classes
Use classes and inheritance
Ver. 1.0 Session 10 Slide 1 of 21
2. Object-Oriented Programming Using C#
Overloading Unary Operators
Unary operators act on a single operand. Some examples
of unary operators are the increment (++) and decrement
(--) operators.
Unary operators can be classified as:
Simple prefix unary operators, for example the – (minus)
operator.
Pre and post increment and decrement operators, for example
the ++() prefix increment operator.
The implementation of the increment operator used with
structs cannot be implemented with classes.
Ver. 1.0 Session 10 Slide 2 of 21
3. Object-Oriented Programming Using C#
Overloading Binary Operators
Binary operators are operators that work with two operands.
Binary operators include the arithmetic operators (+, -, *, /,
%), arithmetic assignment operators (+=, -=, *=, /=. %=),
and comparison operators (<, >, <=, >=, ==, !=).
Overloading a binary operator is similar to overloading a
unary operator, except that a binary operator requires an
additional parameter.
Following is the syntax of binary overloading in C#:
operator <operator>(Object1,Object2);
Ver. 1.0 Session 10 Slide 3 of 21
4. Object-Oriented Programming Using C#
Demo: Overloading an Operator
Problem Statement:
FunCity Land is a newly established amusement park located
in West Virginia. Brad is a chief technical officer of FunCity
Land. He is currently working on a project related with
calculating distances. This project will calculate the distance to
reach FunCity Land from various locations because many
visitors do not know the exact location of the park.
Brad needs to develop a program, which would enter a visitor’s
location and the distance traveled by the visitor till that point.
After analyzing the location and the distance traveled by the
visitor, the program should display the remaining distance and
the total distance traveled.
Help Brad to develop an application by overloading the binary
operator(+).
Ver. 1.0 Session 10 Slide 4 of 21
5. Object-Oriented Programming Using C#
Demo: Overloading an Operator (Contd.)
Solution:
To develop the required application, Brad needs to perform the
following tasks:
1. Create a console-based application.
2. Build and execute an application.
Ver. 1.0 Session 10 Slide 5 of 21
6. Object-Oriented Programming Using C#
Identifying Relationships Between Classes
In software application, classes and objects are related to
each other.
In the object-oriented approach, objects perform actions in
response to messages from other objects defining the
recipient object’s behavior.
This approach specifies the relationships among classes
based on the behavior of individual class.
Ver. 1.0 Session 10 Slide 6 of 21
7. Object-Oriented Programming Using C#
Kinds of Relationships
There are various relationships between objects of different
classes in an object-oriented environment:
Inheritance Relationship:
Object-oriented programming enables classes to inherit commonly
used state and behavior from other classes.
In C# programming, a class is allowed to inherit from another
class.
Generalization is needed to create programs that can be
customized in accordance with new requirements.
Let us understand the concept of inheritance relationship
with the help of an example.
Ver. 1.0 Session 10 Slide 7 of 21
10. Object-Oriented Programming Using C#
Kinds of Relationships (Contd.)
Parent Class
Child Class
Deriving the features of parents,
such as height, complexion, and
Behavior
Ver. 1.0 Session 10 Slide 10 of 21
11. Object-Oriented Programming Using C#
Kinds of Relationships (Contd.)
You can also establish a relationship between a superclass
and its subclasses.
The following figure shows the hierarchy of the subclasses
of a superclass.
Automobile
Car Bus
Ford BMW Toyota Mitsubishi
Ver. 1.0 Session 10 Slide 11 of 21
12. Object-Oriented Programming Using C#
Kinds of Relationships (Contd.)
• Composition Relationship:
Object-oriented programming allows you to form an object,
which includes another object as its part. This mechanism of
forming an object is called composition.
• Utilization Relationship:
Object-oriented programming allows a class to make use of
another class. This kind of relationship is called utilization.
• Instantiation Relationship:
Object-oriented programming allows a relationship between a
class and an instance of that class. This kind of relationship is
called instantiation.
Ver. 1.0 Session 10 Slide 12 of 21
13. Object-Oriented Programming Using C#
Just a minute
Build the hierarchy of the mixer, VCR, color television,
washing machine, stereo class objects, and generalize
wherever possible.
Answer:
The figure shows the hierarchy of the class objects.
Electronic Items
Entertainment Items Utility Items
Color VCR Stereo Washing
Mixer
Television Machine
Ver. 1.0 Session 10 Slide 13 of 21
14. Object-Oriented Programming Using C#
Just a minute
Identify the relationship between the following class pairs:
1. Television – Speaker
2. Mammal – Tiger
3. Garment – Shirt
4. Cup – Tea
5. Computer – Microprocessor
Answer:
5 and 1 are examples of composition relationships.
3 and 2 are examples of inheritance relationships.
4 does not exhibit any relationship. Tea is not an attribute of
cup.
Ver. 1.0 Session 10 Slide 14 of 21
15. Object-Oriented Programming Using C#
Just a minute
Identify the classes and their utilization relationship in the
following scenario of a departmental store.
There are several counters and each counter in the store is
manned by a single salesperson who sells a specific
product. A customer approaches a counter. Depending on
the customer’s desire to purchase a product, the
salesperson sells the product to the customer and accepts
payment.
Answer:
The utilization relationship of the preceding scenario are:
Salespersons man the counters.
Each counter deals in a specific product.
The customer buys a product.
The customer pays the salesperson.
Ver. 1.0 Session 10 Slide 15 of 21
16. Object-Oriented Programming Using C#
Using Classes and Inheritance
In C#, inheritance is the property by which the objects of a
derived class possess copies of the data members and the
member functions of the base class.
A class that inherits or derives attributes from another class
is called the derived class.
The class from which attributes are derived is called the
base class.
In object-oriented programming, the base class is actually a
superclass and the derived class is a subclass.
Ver. 1.0 Session 10 Slide 16 of 21
17. Object-Oriented Programming Using C#
Implementing Inheritance
Each instance of the derived class includes the attributes of
the base class.
Any change made to the base class automatically changes
the behavior of its derived classes.
The syntax used in C# for creating derived classes is as
follows:
<access-specifier> class <base_class>
{
...
}
class <derived_class> : <base_class>
{
...
}
Ver. 1.0 Session 10 Slide 17 of 21
18. Object-Oriented Programming Using C#
Implementing Inheritance (Contd.)
You can use the following thumb rule to establish whether
inheritance is required while you are designing a program.
Always check the kind of relationship between the derived
and the base class.
Ensure that the derived class can base class.
You can also use inherited classes with constructors.
In inheritance, the derived class receives the base class
member variables and methods. This means that an order of
inheritance must be followed during the construction of an
object.
Ver. 1.0 Session 10 Slide 18 of 21
19. Object-Oriented Programming Using C#
Summary
In this session, you learned that:
Operators may be classified as unary and binary operators.
Unary operators work with one operand and are classified as:
• Simple prefix unary operators
• Pre and post increment and decrement operators
– A prefix unary operator may be defined by a member function
that takes no parameters or by a nonmember function that
takes one parameter.
– Binary operators work with two operands.
– Overloading a binary operator is similar to overloading a unary
operator, except that a binary operator requires an additional
parameter.
Ver. 1.0 Session 10 Slide 19 of 21
20. Object-Oriented Programming Using C#
Summary (Contd.)
The four kinds of relationships that exist among classes are:
• Inheritance relationship
• Composition relationship
• Utilization relationship
• Instantiation relationship
– Object-oriented programming enables classes to inherit
commonly used state and behavior from other classes.
– Generalization means that multiple classes can inherit from the
same superclass.
– The composition relationship exists when one class is made up
of another class.
– The utilization relationship exists between two or more
unrelated classes if one class uses the other.
Ver. 1.0 Session 10 Slide 20 of 21
21. Object-Oriented Programming Using C#
Summary (Contd.)
An instantiation relationship is a relationship between a class
and an instance of that class.
A class that inherits or derives attributes from another class is
called the derived class and the class from which it is derived
is called the base class.
Inheritance avoids redundancy in code and enables easy
maintenance of code.
Constructors are called in the order of base-to-derived.
Ver. 1.0 Session 10 Slide 21 of 21
Editor's Notes
Students have learnt the structure of different types of dimensions and the importance of surrogate keys in Module I. In this session, students will learn to load the data into the dimension tables after the data has been transformed in the transformation phase. In addition, students will also learn to update data into these dimension tables. Students already know about different types of dimension tables. Therefore, you can start the session by recapitulating the concepts. Initiate the class by asking the following questions: 1. What are the different types of dimensions? 2. Define flat dimension. 3. What are conformed dimension? 4. Define large dimension. 5. Define small dimension. 6. What is the importance of surrogate key in a dimension table? Students will learn the loading and update strategies theoretically in this session. The demonstration to load and update the data in the dimension table will be covered in next session.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Students know the importance of surrogate keys. In this session students will learn the strategy to generate the surrogate key. Give an example to explain the strategy to generate the surrogate keys by concatenating the primary key of the source table with the date stamp. For example, data from a Product table has to be loaded into the Product_Dim dimension table on Feb 09, 2006. The product_code is the primary key column in the Product table. To insert the surrogate key values before loading the data into the dimension table, you can combine the primary key value with the date on which the data has to be loaded. In this case the surrogate key value can be product_code+09022006.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.