LAB 4 - DCE Ali
LAB 4 - DCE Ali
IBIT
Lab4
Create a class Student and declare private variables Name, regNumber, degree of type string and cgpa of type float. Write a constructor function for student which takes values from user. Create another class for main function and create object of type student. {Constructor ko bar bar call nai ke sakte Ye reentry ha .ye fun change kare ga . get function return kerwae gaye sare class me likhne hain or main me object ke sath karine hain}
using System; using System.Collections.Generic; //using System.Linq; using System.Text; namespace Project1 { class student { public void setname() { name= Console.ReadLine(); public void setregname() regname= Console.ReadLine(); public void setdegree() degree = Console.ReadLine() public void setcgpa()
Additions 1. 2. 3. 4. 5. Add getter and setter functions for name Add getter and setter functions for regNumber Add getter and setter function for degree Add getter and setter function for cgpa Add a function that prints all the information of object on console.
IBIT
Lab4
Question # 2 Inheritance Implement Base Classes. Implement Derived Classes. Initialize Base Classes from Derived Classes. Learn How to Call Base Class Members. Learn How to Hide Base Class Members. Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective employment of reuse, you can save time in your programming. Execute the following code to understand the flow of information 1. Create an empty project. 2. Add class to the project and name it Parent.cs 3. Add public with the class and then type the following code in parent class
4.
public Parent() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); }
5. Add another class to the project and name it Child.cs 6. For inheritance public class Child : Parent 7. Add public with the class and type the following code in child class.
8.
public Child() { Console.WriteLine("Child Constructor."); }
9. Add class for main and name it demo.cs 10. Create the object for child class as follows
public static void Main() { Child child = new Child(); child.print(); Console.ReadLine(); }
Now go to Build tab and build the solution and if there is no compilation errors then execute the code and observe the order of function calls. //
IBIT
Lab4
public void setname() { Console.WriteLine("enter ur name "); name= Console.ReadLine(); } public void setregname() { Console.WriteLine ("enter ur regnum"); regnum= Console.ReadLine(); } public void setdegree() { Console.WriteLine("enter ur degree"); degree = Console.ReadLine(); } public void setcgpa() { Console.WriteLine("enter ur cgpa "); cgpa= Convert.ToDouble(Console.ReadLine());
} } }
using System; using System.Collections.Generic; //using System.Linq; using System.Text; namespace check
IBIT
Lab4
static void Getname() { student obj = new student();// student ke name se jo ha us ka obj banate hain . obj.setname(); obj.setregname(); obj.setcgpa(); obj.setdegree();
class bani
} } }