Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

In OOP you create the following activities

Create a class
Create Objects from the class

C# Class
Class is blueprint for the object

To create a class you use the keyword class

Syntax

Class ClassName {

// fields – variables to store data


// methods – functions to perform tasks

internal class Cube


{
int side=5;
int getVolume()
{
return side*side* side;
}

static void Main(string[] args)


{
Cube Cube1 = new Cube();
Console.WriteLine(Cube1.getVolume());
Console.WriteLine(Cube1.side);
}
}
namespace student
{
internal class Student //
{
double CA = 0; //fields or attribute
double ExamMark = 0;
string Sname = "";

// method to calculate finalmark


double getFinalMark()
{
return 0.4 * CA + 0.6 * ExamMark;
}

static void Main(string[] args)


{

Student Student1 = new Student(); // object


Student Student2 = new Student();
Student1.CA = 50;
Student1.ExamMark = 67;
Student1.Sname = "Trevor";

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);
}

static void Main(string[] args)


{
Rectangle R1 = new Rectangle(); // object creation
Rectangle R2 = new Rectangle();
R2.Length = 3;
R2.Width = 2;

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;
}

static void Main(string[] args)


{
Cube Cube1 = new Cube(); // object creation
Cube Cube2 = new Cube();
Cube1.side = 1;
Cube1.colour = "purple";

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.

You might also like