0% found this document useful (0 votes)
7 views42 pages

C# Object Oriented Programming (OOP)- Chapter 1

C# Object Oriented Programming (OOP)- Chapter 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views42 pages

C# Object Oriented Programming (OOP)- Chapter 1

C# Object Oriented Programming (OOP)- Chapter 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

C# Object Oriented

programming (OOP)

Education and Training Solutions 2022


1 Overview of OOP

2 OOP Structure

3 Encapsulation

4 Constructor

5 Constructor Overloading
Overview of OOP
Overview of OOP

❑ The process of organize software design around data or objects, rather than
functions and logic is called Object Oriented Programming.
❑ Large, complex, and actively updated programs are a good fit for this method
of development.
❑ OOP revolve around the real-life entities.
OOP programming languages

• C#
• Python
• C++
• PHP
• TypeScript
OOP benefits

❑ Reusability: means that you don’t have to write code multiple times, Instead,
You can reuse it through inheritance.
❑ Modularity: it is about partition the code into modules, building them first
followed by linking and finally combining them to form a complete project.
OOP benefits

❑ Security: as the complex code is hidden, software maintenance is easier and


internet protocols are protected.
❑ Productivity: construct new programs quicker using multiple libraries and
reusable code.
OOP structure
Main aspects of OOP

❑ Class: user-defined datatype act as the blueprint for the object, that contains
functions to perform operations on the data and variables for storing data.
❑ Object: it is an instance of a class which specify real-world data.
❑ The classes, objects, and its attributes and methods is the main component of
OOP.
Class and object

In real life, a car is an object. The car has attributes or field, such as weight, size and
color, and methods, such as drive and brake.
Class

Objects
Class and object

❑ To create a class, we should use the class keyword while if we want to create
an object of a class then we should use new keyword.
❑ You can access the class members once you create the object using this object
name.
Let’s Take an Example
Define a class

public class Car


{
string type;
string weight;
string color;
public void Drive(){ Console.WriteLine("Car is
driving");
}
public void Brake()
{ Console.WriteLine("Car is
Breaking");
}
}
// in main
Car car = new Car();
Gift for you😊

❑ When you want to name an object, variable or parameter use Camel case
naming convention (A word with the first letter lowercase, and the first letter
of each subsequent word-part capitalized. Ex: customerName,
employeeDetails, salary).
Gift for you😊

❑ As well as, if you want to name a class, methods or properties use Pascal case
naming convention (A word with the first letter capitalized, and the first letter
of each subsequent word-part capitalized. Ex: CustomerName,
EmployeeDetails, Salary, etc.).
Fields

❑ It is a class variable; we can access it by creating an object from the class.


❑ It is private by default if you don’t specify access modifier.
Properties

❑ It is an extension of the class variable, it provides a mechanism to read, write


or change the value of the class variable without affecting the external way of
accessing it in our applications.
Properties

❑ In c#, properties can contain one or two code blocks


called accessors get accessor and set accessor.
❑ By using get and set accessors, we can change the internal implementation of
class variables and expose it based on our requirements.
Encapsulation

❑ One of the main concept of OOP, It is the process of making sure that the
sensitive data are hidden from the users.
❑ This can be achieved by declaring variable as private, then use access modifier
to read or write into it.
Let’s Take an Example
This is how we can define a propriety

string type;
int weight;
string color;
public string Type{ get=> type; set=>
type=value; }
public int Weight{ get=> Weight; set=>
weight=value; }
public string Color
{ get=>color; set=> color=value; }
Constructor
Instance constructor

❑ The instance constructor is special type of method that get executed when
an instance of the class is created. Programmers usually use it to specify the
fields that are declared in class for each object is created from. It is public
by default and has no return type.
❑ Constructor usually used to Initialize the instance variables.
Let’s Take an Example
Instance constructor

public Car(){
Console.WriteLine("This is
constructor");
}
}
Constructor overloading
Constructor Overloading

❑ It is the ability to define a Constructor in different form.


❑ We can overload the constructor using same name with different parameters
(different type of parameters, different number of parameters or different
order of parameters).
Constructor Overloading

❑ It is the ability to define a Constructor in different form.


❑ We can overload the constructor using same name with different parameters
(different type of parameters, different number of parameters or different
order of parameters).
Let’s Take an Example
Constructor overloading

public class Car {

public Car(){ Console.WriteLine("This is


constructor1");}

public Car(string color){


Console.WriteLine("This is constructor2");
}
This is how we can define a propriety

public Car(string color, string type){


Console.WriteLine("This is
constructor3");
}
}
Example
Example

Square and rectangle is an object, so we can represent “shape” as class of these


object, it has height, width and color as properties, area and circumference as
methods
Define shape class

public class Shapes


{
public Shapes(double shapeheight, double
shapewidth)
{
ShapeHeight = shapeheight;
ShapeWidth = shapewidth;
}
public double ShapeWidth { get; set; }
public double ShapeHeight { get; set; }
public int ShapeColor { get; set; }
public double ShapeArea() {
return ShapeWidth * ShapeHeight;}
}
Create object from shape class

static void Main(string[] args)


{
Shapes Rectangle = new Shapes(5, 4);
double area = Rectangle.ShapeArea();
Console.WriteLine($"Rectangle area=
{area}");
}
The Learning Hub Copyright
2022- All Right Reserved

1. Create a class called “Number” that have 2 int


numbers as proprieties initially set to zero .
2. Create method called “calculate” that find the result
of adding, subtracting, dividing and multiplying this 2
numbers.
Exercises
The Learning Hub Copyright
2022- All Right Reserved

3. Create other method called “IsOdd” that return true if


the 2 number is odd and false if one of them is even.

Exercises
Exercise Solution 1

public class Number


{
public int num1 { get; set; }
public int num2 { get; set; }
public Number(int num1, int num2)
{
this.num1 = num1;
this.num2 = num2;
}}
Exercise Solution 2

public void Calculate()


{
Console.WriteLine($"num1 + num2 = {num1+
num2}");
Console.WriteLine($"num1 * num2 = {num1*
num2}");
Console.WriteLine($"num1 / num2 = {num1/ num2}");
Console.WriteLine($"num1 - num2 = {num1-
num2}");
Exercise Solution 3

public bool IsOdd()


{
if (num1 % 2 == 0)
{
return false;
}
else
return true;
}
Exercise Solution 3 - main

static void Main(string[] args)


{
Number number = new Number(2, 2);
number.Calculate();
if (number.IsOdd())
{
Console.WriteLine("The 2 numbers are
odd");
}
else {
Console.WriteLine("One or both of
the 2 numbers are even");
}
}

You might also like