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

Introduction To Classes in OOP

The document introduces object-oriented programming concepts like classes, objects, and methods. It provides coding exercises to create a Student class with properties like name, roll number, and GPA. The exercises demonstrate creating multiple class objects and accessing object properties. Further concepts covered include taking user input for class properties and creating a student management system using classes.

Uploaded by

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

Introduction To Classes in OOP

The document introduces object-oriented programming concepts like classes, objects, and methods. It provides coding exercises to create a Student class with properties like name, roll number, and GPA. The exercises demonstrate creating multiple class objects and accessing object properties. Further concepts covered include taking user input for class properties and creating a student management system using classes.

Uploaded by

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

Object Oriented Programming

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.

Students, Object-Oriented Programming is different from Procedural programming as it


is about creating objects that contain both data and methods.

Let's do some coding.

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.

Task: Write a program that creates a new class of students.


Solution:
Write the following code before the main function of the code and execute the program
by clicking on the start button.

Code:

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

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.

students s1 = new students();

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

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:

Multiple Class Objects

Just like we learned to use Structs to create multiple objects of “User-Defined


DataType”, Similarly, the class is also a “User Defined DataType” that is used to create
multiple objects having the same properties but different values. Let's try to understand
this concept through coding.

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:

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

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.

string name = s1.name;


Console.WriteLine(“Name: {0}”, name);

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

Taking input from User in Class Object


Taking input in class object variables is the same as taking input in any other variables in
C#.
Look at the following code snippet to have a clear understanding of this concept.

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:

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

Student Management System with Class


Task: Write a program that shows three menu options
1. Add Student.
2. Show Students.
3. Top Students.

● 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:

Sr. Action Description


#

1. Create a separate class in a new folder


named “BL”, with public data members.

Note: The objective of OOP is that we


want to keep our code and class data
separate. Therefore, this is the preferred
practice that we will adopt in the
coming lectures.

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

2. Creates the main menu function.

3. 1. Creates a function that takes the


following inputs from the user
● Name
● Roll Number
● CGPA
● Department
● IsHostelide
2. Creates a class “students” type
object and stores the input in that
object
3. Returns this object to the main
function so it can be stored into
the main array inside the main
function.

4. Receives complete arrays of students


and prints the information in line by line
manner.

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

5. Prints the first three students from the


list.

6. Finds and returns the index of the


largest item from the array.

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

7. Invokes the respective functionality


according to input provided by the user.

Department of Computer Science, UET Lahore.


Object Oriented Programming
Lab Manual 2

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.

Good Luck and Best Wishes !!


Happy Coding ahead :)

Department of Computer Science, UET Lahore.

You might also like