Classes and Objects In C# With Example
-
Upload
cheezy-code -
Category
Software
-
view
439 -
download
1
Transcript of Classes and Objects In C# With Example
Car beetle = new Car();
class Car {
//Propertiespublic int modelYear;public string carName;public string carType;public float price;
//Methodspublic void DriveCar(){
Console.WriteLine("Car is driving");}public void ApplyBrake(){
Console.WriteLine("Car is applying brakes");}
}{CHEEZYCODE}
Car beetle = new Car(); 1
class Car {
//Propertiespublic int modelYear;public string carName;public string carType;public float price;
//Methodspublic void DriveCar(){
Console.WriteLine("Car is driving");}public void ApplyBrake(){
Console.WriteLine("Car is applying brakes");}
} {CHEEZYCODE}
Car beetle = new Car(); 1 When we say new Car(), new object in the memory is created.
class Car {
//Propertiespublic int modelYear;public string carName;public string carType;public float price;
//Methodspublic void DriveCar(){
Console.WriteLine("Car is driving");}public void ApplyBrake(){
Console.WriteLine("Car is applying brakes");}
}{CHEEZYCODE}
Car beetle = new Car(); 1 When we say new Car(), new object in the memory is created.
Memory Locations
{CHEEZYCODE}
Car beetle = new Car(); 1 When we say new Car(), new object in the memory is created.
Object will occupy certain bytes in memory
Memory Locations
{CHEEZYCODE}
Car beetle = new Car(); 1 When we say new Car(), new object in the memory is created.
Memory Locations
{CHEEZYCODE}
Car beetle = new Car(); 1 When we say new Car(), new object in the memory is created.
Memory Locations
{CHEEZYCODE}
Car beetle = new Car(); 1 When we say new Car(), new object in the memory is created.
This is new object of Car in memory
Memory Locations
{CHEEZYCODE}
Car beetle = new Car(); 2
This is new object of Car in memory
Memory Locations
Object has been created, now location of this object is passed as a reference
{CHEEZYCODE}
Car beetle = new Car(); 3
This is new object of Car in memory
Memory Locations
Location of this object is stored in beetle to access the car object
{CHEEZYCODE}
Car beetle = new Car(); 4
beetle
Memory Locations
Now you can access newly created object in memory with the help of beetle reference
{CHEEZYCODE}
Car beetle = new Car(); 5
beet le
What does this object has?
class Car {
//Propertiespublic int modelYear;public string carName;public string carType;public float price;
//Methodspublic void DriveCar(){
Console.WriteLine("Car is driving");}public void ApplyBrake(){
Console.WriteLine("Car is applying brakes");}
}
Remember this ?
{CHEEZYCODE}
Car beetle = new Car(); 6
beet le
Object stores properties values - it could be another object or built in types like int or float
class Car {
//Propertiespublic int modelYear;public string carName;public string carType;public float price;
//Methodspublic void DriveCar(){
Console.WriteLine("Car is driving");}public void ApplyBrake(){
Console.WriteLine("Car is applying brakes");}
} {CHEEZYCODE}
7beet le beetle object in memory has these properties
class Car {
//Propertiespublic int modelYear;public string carName;public string carType;public float price;
//Methodspublic void DriveCar(){
Console.WriteLine("Car is driving");}public void ApplyBrake(){
Console.WriteLine("Car is applying brakes");}
}
modelYear (Integer)
carName (String)
carType(String)
price(float)
{CHEEZYCODE}
8 What if I want to create 2 objects?
Car beetle = new Car();
Car mustang = new Car();
Same process will be followed
{CHEEZYCODE}
8 What if I want to create 2 objects?
modelYear (Integer)
carName (String)
carType(String)
price(float)
modelYear (Integer)
carName (String)
carType(String)
price(float)
beet le mustang
{CHEEZYCODE}
8
beet le
What if I want to create 2 objects?
modelYear (Integer)
carName (String)
carType(String)
price(float)
modelYear (Integer)
carName (String)
carType(String)
price(float)
mustang
BOTH OBJECTS
ARE DIFFERENT
{CHEEZYCODE}
9Assign values to these properties using dot(.) operator
beet le
modelYear (Integer)
carName (String)
carType(String)
price(float)
Assigning values to these properties. These are similar to variables but they are inside beetle and part of beetle object.
{CHEEZYCODE}
9Assign values to these properties using dot(.) operator
beet le
modelYear (Integer)
carName (String)
carType(String)
price(float)
beetle.modelYear = 2017;
beetle.carName = "Volkswagen Beetle“;
beetle.carType = "Beetle 1.8T “;
beetle.price = 19795F;
{CHEEZYCODE}
10Access these properties using dot operator
beet le
modelYear (Integer)
carName (String)
carType(String)
price(float)
You access the properties of beetle using dot operator
{CHEEZYCODE}
10Assign values to these properties using dot(.) operator
beet le
modelYear (Integer)
carName (String)
carType(String)
price(float)
Console.WriteLine(beetle.modelYear);
and similarly you access other properties of object
{CHEEZYCODE}