Introduction To Classes in OOP
Introduction To Classes in OOP
Lab Manual 2
Introduction
After a week of rigorous coding, Welcome back!
You have learned all about the C# in the previous labs and manuals.
Let's move on to the next, new, and interesting concepts.
Class Declaration
We have learned in the previous manual about the basic code that visual studio provides
and programmers start the work from the main function directly.
Syntax:
class class_name
{
// class_members
}
This code is written outside the “main function” and inside the “class program” and it
creates a new class in the program. To understand this concept, try writing the following
program.
Code:
The code will generate a new class of students where each student would have the
following properties.
● string type Name
● int type Roll Number
● float type CGPA
It can include many other properties however, for simplicity these work with these three
characteristics at the time.
Now, in order to create a “new object” of class students, we will declare a class type
object in the main function.
This line will “create a new object of class students” having the above-defined
properties. To understand this concept, try assigning values to the s1 variable.
Task: Write the code that creates an object and assign values to a class object.
Solution:
Write the following code into the main function of the code and execute the program by
clicking on the start button.
Code: Output:
Task: Write the code to create multiple class objects and assign values to all of them.
Solution:
Write the following code into the main function of the code and execute the program by
clicking on the start button.
Code: Output:
Observe that each object possesses the same properties however, we have assigned
different values to the same variables. The output reflects that all variables belong to a
separate “class object” and therefore can be assigned new values in other class objects.
We access the variables by using the (dot .) operator in front of the class object name. For
example, to print the name of the s1 student on the console, we will use the following
code.
Task: Write the code to create a class object and take user name, roll number, and CGPA
from the user and store them in the class object.
Solution
Write the following code on your computer and execute the program by clicking on the
start button.
Code: Output:
● Add Student allows users to add a student’s information that includes RollNo,
Name, GPA, isHostelide, Department.
● Show Student displays all the added students on the screen.
● Top Student lists the information of the top 3 students.
Solution:
Challenge # 1:
Task: Write a program that shows three menu options
1. Add Products.
2. Show Products.
3. Total Store Worth.
● Add Product allows the user to add product information that includes ID, Name,
price, Category, BrandName, Country.
● Show Product display all the added products on the screen.
● Total Store Worth calculates the sum of the price of all the products.
Challenge # 2:
Task Covert the signUp/signIn application that you developed in the previous lab by
using the class concepts.
Make a class named Credentials with two attributes namely
● Username
● Password
The data should be loaded from the file and loaded into the attributes of the class.