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

OOPs Pratice Program

Uploaded by

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

OOPs Pratice Program

Uploaded by

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

Oops Questions

Public – Visible in the current and referencing assembly.


Private – Visible inside the current class.
Protected – Visible inside the current as well as in the derived class.
Internal – Visible inside the containing assembly.
Internal protected – Visible inside the containing assembly, also in the descendant
of the current class.

modifiers which refine the declaration of a class. Here is the list of all
supported modifiers in C#.

Sealed – The class stops inheriting by any derived class.


Static – The class contains only static members.
Unsafe – The class that stores unsafe types likes pointers.
Abstract – No instance of the class if the Class is abstract.

Q1> using System;


public class emp
{
public static int age = 40;
public static int salary = 25000;

}
public class record :emp
{
new public static int salary = 50000;
static void Main(string[] args)
{
Console.WriteLine(emp.age + " " + emp.salary + " " + salary);
}
}

Q2> class baseclass


{
public void func1() {}
public virtual void func2() {}
public virtual void func3() {}
}
class derivedclass :baseclass
{
new public void func1() {}
public override void func2() {}
public new void func3() {}
}
class Program
{
static void Main(string[] args)
{
baseclass b = new derivedclass();
b.func1 ();
b.func2 ();
b.func3 ();
}
}
Q3>using System;
class BaseClass
{
public virtual void Print()
{
System.Console.WriteLine("BaseClass");
}
}

class DerivedClass : BaseClass


{
public override void Print()
{
System.Console.WriteLine("DerivedClass");
}
}

class Derived2Class : DerivedClass


{
public new void Display()
{
System.Console.WriteLine("Derived2Class");
}
}

class Program
{
public static void Main()
{
BaseClass b;
b = new Derived2Class();
b.Print();
}
}

Q4>Create a C# program that requests three names of people from the user and stores
them in an array of objects of type Person. To do this, first create a Person class
that has a Name property of type string and override the ToString() method.

End the program by reading people and executing the ToString() method on the
screen.

Input

Juan
Sara
Carlos

Output

Hello! My name is Juan


Hello! My name is Sara
Hello! My name is Carlos

using System;
public class FirstClass
{
public static void Main(string[] args)
{
int total = 3;
Person[] persons = new Person[total];

for (int i = 0; i < total; i++)


{
persons[i] = new Person() {
Name = Console.ReadLine()
};
}

for (int i = 0; i < total; i++)


{
Console.WriteLine(persons[i].ToString());
}

Console.ReadLine();
}

public class Person


{
public string Name { get; set; }

public override string ToString()


{
return "Hello! My name is " + Name;
}
}
}

You might also like