Classes and Objects-1
Classes and Objects-1
Create a class
Create Objects from the class
C# Class
Class is blueprint for the object
Syntax
Class ClassName {
Student2.CA = 100;
Student2.ExamMark = 7;
Student2.Sname = "Dineo";
Console.WriteLine(Student1.CA);
Console.WriteLine(Student1.ExamMark);
Console.WriteLine(Student1.Sname);
Console.WriteLine(Student1.getFinalMark());
Console.WriteLine("***************");
Console.WriteLine("***************");
Console.WriteLine(Student2.Sname);
Console.WriteLine(Student2.CA);
Console.WriteLine(Student2.ExamMark);
Console.WriteLine(Student2.getFinalMark());
}
}
namespace Rectangle
{
internal class Rectangle //
{
int Length; //fields or attribute
int Width;
string colour;
int getArea()
{
return Length* Width;
}
int getPerimeter()
{
return 2* (Length + Width);
}
Console.WriteLine(R2.Length);
Console.WriteLine(R2.Width);
Console.WriteLine(R2.getArea());
Console.WriteLine(R2.getPerimeter());
Console.WriteLine("***************");
Console.WriteLine(R1.colour);
Console.WriteLine(R1.Length);
Console.WriteLine(R1.Width);
Console.WriteLine(R1.getArea());
Console.WriteLine(R1.getPerimeter());
}
}
namespace cube
{
internal class Cube //
{
int side; //fields or attribute
string colour;
//method
int getVolume()
{
return side * side * side;
}
Cube2.side = 20;
Cube2.colour = "red";
Console.WriteLine(Cube1.side);
Console.WriteLine(Cube1.colour);
Console.WriteLine(Cube1.getVolume());
Console.WriteLine("***************");
Console.WriteLine(Cube2.side);
Console.WriteLine(Cube2.colour);
Console.WriteLine(Cube2.getVolume());
}
}
Create a class called Circle that includes the radius of the circle (type int) as an instance
variable. Your class should have a constructor that initializes the data member. Provide a set
and a get function for the data member. The class should have two methods; Method Area (),
(to calculate the area of the circle), and method circumference (), (to calculate the
circumference of the circle).
ii. Create two objects of the class defined above.
iii. Display the Area and the radius of objects created above. [12 marks]
Create a class called Invoice that a hardware store might use to represent an invoice for an
item sold at store. An Invoice should have three data members- part number (type string), the
quantity of the item being purchased (type int), and a price per item (type int). ur class should
have a constructor that initialises the data members. Provide a set and a get function for the
data member. In addition, provide a method named getInvoiceAmount that calculates the
invoice amount (i.e multiplies the quantity by the price per item), then returns the amount as an
int value. If the price per item is not positive, it should be set to 0. Write a test program that
demonstrates class Invoice’s capabilities.