0% found this document useful (0 votes)
27 views

Computer Applications in Industrial Engg.-I: Lecture #08

The document summarizes key concepts about classes in C#, including: 1) A class acts as a template that defines the properties and methods of an object, while an object is an instance of a class created at runtime. 2) Examples of classes include House, Car, Bank Account, and Student. 3) To add a class in C#, a class is declared within a namespace with properties and methods defined within the class. 4) An object of the class is instantiated using the new keyword, and its methods can then be called using dot notation on the object.

Uploaded by

atifshahzad
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Computer Applications in Industrial Engg.-I: Lecture #08

The document summarizes key concepts about classes in C#, including: 1) A class acts as a template that defines the properties and methods of an object, while an object is an instance of a class created at runtime. 2) Examples of classes include House, Car, Bank Account, and Student. 3) To add a class in C#, a class is declared within a namespace with properties and methods defined within the class. 4) An object of the class is instantiated using the new keyword, and its methods can then be called using dot notation on the object.

Uploaded by

atifshahzad
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 46

DR ATIF SHAHZAD

IE-322

Computer Applications
LECTURE #08 in Industrial Engg.-I
Computer Applications in
Industrial Engg.-I

IE322
Dr. Atif Shahzad

8/12/2018
…Recap
What we will see…
Files of the project in
A Random Review Solution Explorer
Windows explorer

Complete Example—
A quick Question Example2:
class Car

Classes
• Class vs Object: Calling a Method of an
Some built-in classes
Examples object
• Class vs Object:

A Bonus Topic
Adding a Class to a C# Instantiating an object • Look this example Project & Solution
project of a class • Conditional Operator
Dr. Atif Shahzad

Adding a Method to the What is a Visual Studio What is a Visual Studio


Declaring a Class
Class Project? Solution?

8/12/2018
A Random Review

Data types are sets


(ranges) of values that
have similar
characteristics.
Dr. Atif Shahzad

5
A Random Review
Data Types DefaultValue Minimum Value MaximumValue

sbyte 0 -128 127


byte 0 0 255
short 0 -32768 32767
ushort 0 0 65535
int 0 -2147483648 2147483647
uint 0u 0 4294967295
long 0L -9223372036854775808 9223372036854775807
ulong 0u 0 18446744073709551615
float 0.0f ±1.5×10-45 ±3.4×1038
double 0.0d ±5.0×10-324 ±1.7×10308
decimal 0.0m ±1.0×10-28 ±7.9×1028
bool false Two possible values: true and false

char '\u0000' '\u0000' '\uffff'


object null
Dr. Atif Shahzad

- -

string null - -

6
A Random Review: Integer types
// Declare some variables
byte centuries = 20;
ushort years = 2000;
uint days = 730480;
ulong hours = 17531520;
// Print the result on the console
Console.WriteLine(centuries + " centuries are " + years +
" years, or " + days + " days, or " + hours + " hours.
");

// Console output:
// 20 centuries are 2000 years, or 730480 days, or 17531520
// hours.

ulong maxIntValue = UInt64.MaxValue;


Console.WriteLine(maxIntValue); // 18446744073709551615
Dr. Atif Shahzad

7
A Random Review: Real types
// Declare some variables
float floatPI = 3.141592653589793238f;
double doublePI = 3.141592653589793238;

// Print the results on the console


Console.WriteLine("Float PI is: " + floatPI);
Console.WriteLine("Double PI is: " + doublePI);

// Console output:
// Float PI is: 3.141593
// Double PI is: 3.14159265358979
Dr. Atif Shahzad

8
A quick Question
int age = 8;
if ( !(age >= 16) )
{
Console.Write("Your age is less than 16");
}
// What is the OUTPUT?
Dr. Atif Shahzad

8/12/2018 9
A quick Question
int age = 8;
if ( !(age >= 16) ) {
Console.Write("Your age is less than 16");
}

// Outputs "Your age is less than 16"


Dr. Atif Shahzad

8/12/2018 10
Classes

IE322
Classes
Classes act as templates from which
an instance of an object is created at run time.
Dr. Atif Shahzad

8/12/2018 12
Classes
Classes act as templates
from which
an instance of an object is created
at run time.
Classes define the properties of the object
and the methods used to control the
object's behavior.
Dr. Atif Shahzad

8/12/2018 13
Class vs Object: Example 1
Drawing of House
House
Dr. Atif Shahzad

14
Class vs Object: Example 2
Engineering Drawing of a car
Car
Dr. Atif Shahzad

15
Class vs Object: Example 3
Bank Account class
Bank Account of a customer
Dr. Atif Shahzad

16
Class vs Object: Example 4
Student class
A particular student
Dr. Atif Shahzad

17
Class vs Object:You examples?
Class:
Object:
Attributes:
Methods:
Dr. Atif Shahzad

18
Class vs Object:
Class:
Object:
Attributes or States or Properties
these are the characteristics of the object which define it in a
way and describe it in general or in a specific moment
Methods or Behaviors
these are the specific distinctive actions, which can be done by
the object.
Dr. Atif Shahzad

Methods:

19
Adding a Class to a C# project
Dr. Atif Shahzad

20
Declaring a Class
Let us create a class named as Car
namespace CarApp
{
class Car
{

}
}
Dr. Atif Shahzad

21
Declaring a Class
Let us create a class named as Car
namespace CarApp
{
class Car
{

} // end class Car


}//end namespace CarApp
Dr. Atif Shahzad

22
Adding a Method to the Class

namespace CarApp
{
class Car
{
public void Start()
{ // A method
Console.WriteLine("Car has been
started");
}
Dr. Atif Shahzad

} // end class Car


}//end namespace CarApp 23
Instantiating an object of a class
In the Main method of the Program:
Car MyCar = new Car();
Dr. Atif Shahzad

24
Calling a Method of an object
Access or dot (.) operator

MyCar.Start();
Dr. Atif Shahzad

25
Complete Example—class Car
Dr. Atif Shahzad

26
Complete Example—class Program
Dr. Atif Shahzad

27
Solution Explorer
Dr. Atif Shahzad

28
Files of the project in Windows
explorer
Dr. Atif Shahzad

8/12/2018 29
Example2: public class Cat
{
public class Cat
{
// Field name
// Field name
private string name; private string name;
// Field color // Field color
private string color; private string color;

public string Name public string Name


{ {
// Getter of the property "Name" // Getter of the property "Name"
get get
{ {
return this.name; return this.name;
} }
// Setter of the property "Name" // Setter of the property "Name"
set set
{ {
this.name = value; this.name = value;
} }
} }

public string Color public string Color


{ {
// Getter of the property "Color" // Getter of the property "Color"
get get
{ {
return this.color; return this.color;
} }
// Setter of the property "Color" // Setter of the property "Color"
set set
{ {
this.color = value; this.color = value;
} }
} }

// Default constructor // Default constructor


public Cat() public Cat()
{ {
this.name = "Unnamed"; this.name = "Unnamed";
this.color = "gray"; this.color = "gray";
} }

// Constructor with parameters // Constructor with parameters


public Cat(string name, string color) public Cat(string name, string color)
{ {
this.name = name; this.name = name;
this.color = color; this.color = color;
} }

// Method SayMiau // Method SayMiau


public void SayMiau() public void SayMiau()
{ {
Console.WriteLine("Cat {0} said: Miauuuuuu!", name); Console.WriteLine("Cat {0} said: Miauuuuuu!
} ", name);
}
Dr. Atif Shahzad

}
}

30
Some built-in classes
System.Console
System.String (string in C#)
System.Int32 (int in C#)
System.Array
System.Math
System.Random
Dr. Atif Shahzad

8/12/2018 31
A Bonus Topic

IE322
A Bonus Topic
Conditional Operator
Dr. Atif Shahzad

8/12/2018 33
Look at this example
int age = 42;
string msg;
if(age >= 18)
msg = "Welcome";
else
msg = "Sorry";

Console.WriteLine(msg);
Dr. Atif Shahzad

8/12/2018 34
Conditional Operator
int age = 42;
string msg;
msg = (age >= 18) ? "Welcome" :
"Sorry";
Console.WriteLine(msg);
Dr. Atif Shahzad

8/12/2018 35
Conditional Operator
What is the value of x after this code?
int x = 5;
int y = 3;
x = (x > y) ? y : x;
Dr. Atif Shahzad

8/12/2018 36
Annex-
A Quick random revision

IE322
What is a Visual Studio Project?

When you create an


app, website, plug-in,
etc. in Visual Studio,
you start with a project.
Dr. Atif Shahzad

8/12/2018 38
What is a Visual Studio Project?
When you create an app, website, plug-in, etc.
in Visual Studio, you start with a project.
In a logical sense, a project contains all the
source code files, icons, images, data files, etc.
that are compiled into an executable, library, or
website.
A project also contains compiler settings and
other configuration files that might be needed
by various services or components that your
Dr. Atif Shahzad

program communicates with.


8/12/2018 39
What is a Visual Studio Project?
A project is defined in an XML file with an
extension such as .vbproj, .csproj, or
.vcxproj.
This file contains a virtual folder hierarchy,
and paths to all the items in the project. It
also contains the build settings.
Dr. Atif Shahzad

8/12/2018 40
What is a Visual Studio Project?
In Visual Studio, the project file is used by
Solution Explorer to display the project
contents and settings.
When you compile your project, the
MSBuild engine consumes the project file
to create the executable. You can also
customize projects to produce other kinds
of output.
Dr. Atif Shahzad

8/12/2018 41
What is a Visual Studio Solution?
A project is contained within a solution. A
solution contains one or more related projects,
along with build information, Visual Studio
window settings, and any miscellaneous files
that aren't associated with a particular project.
A solution is described by a text file (extension
.sln) with its own unique format; it is generally
not intended to be edited by hand.
Dr. Atif Shahzad

8/12/2018 42
What is a Visual Studio Solution?
A solution has an associated .suo file that stores
settings, preferences and configuration
information for each user that has worked on
the project.
Dr. Atif Shahzad

8/12/2018 43
Project & Solution
Dr. Atif Shahzad

8/12/2018 44
NEVER hesitate to
contact should you
have any question
Dr. Atif Shahzad
Dr. Atif Shahzad

8/12/2018 46

You might also like