using System;
namespace inhetance
{
class cal
{
public int a, b, c;
public int sum(int x, int y)
{
a = x;
b = y;
c = a + b;
return (c);
}
public int sub(int x, int y)
{
a = x;
b = y;
c = a - b;
return (c);
}
public int div(int x, int y)
{
a = x;
b = y;
c = a / b;
return (c);
}
public int mul(int x, int y)
{
a = x;
b = y;
c = a * b;
return (c);
}
}
class scal : cal
{
public void factorial(int n)
{
int i,fact = 1;
for (i = n; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("factorial value of n is:" +fact);
}
}
class Program
{
static void Main(string[] args)
{
cal x = new cal();
int a, b,c;
Console.WriteLine("Enter the number for a & b=:");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
x.sum(a, b);
x.sum(a,b);
x.mul(a, b);
x.div(a, b);
Console.WriteLine("sum of a & b is"+x.sum(a,b));
Console.WriteLine("sum of a & b is" + x.sub(a, b));
Console.WriteLine("sum of a & b is" + x.mul(a, b));
Console.WriteLine("sum of a & b is" + x.div(a, b));
scal x1 = new scal();
int n;
Console.WriteLine("Enter no for factorial value:");
n = int.Parse(Console.ReadLine());
x1.factorial(n);
Console.ReadKey();
}
}
}
Output:
Enter the number for a & b=:
12
3
sum of a & b is15
sum of a & b is9
sum of a & b is36
sum of a & b is4
Enter no for factorial value:
7
factorial value of n is:5040
YOU have wrote two time this code :
ReplyDeletex.sum(a, b);
x.sum(a,b);
It should be :
x.sub(a, b); <--- (you forgot to write sub)
x.sum(a,b);
Please make changes
Inheritance
ReplyDeleteDef-1. Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them.
Def-2. Inheritance is almost like embedding an object into a class.
Def-3. Inheritance lets you include the names and definitions of another class’s members as part of a new class. The new class contains a subobject of the type of the base class.
Def-4. Default association of base class in the derived class.
Inheritance--
1. It is the process by which a class can reuse and extend the existing class without modifying them.
2. It is like embedding an object into a class.
3. It is a default association of base class in the derived class.
4. It gives us the ability to include the name and definition of the members into a new class. New class has or contains sub-object of base type.
5. It provides a mean of encapsulation.
6. We don't need to create the object of base class in the derived class to call it's functions in the derived class because the memory allocation of base class is implicitly took place in the derived class.
7. Without inheritance reusability is also achieved but a class don't get the behavior of a class.
Example: - without using Inheritance
using System;
class A
{
int data;
public void f(int arg)
{
data = arg;
}
public int g()
{
return data;
}
}
class B
{
public A obj1 = new A();
public static void Main()
{
B obj2 = new B();
obj2.obj1.f(20);
Console.WriteLine(obj2.obj1.g());
Console.ReadLine();
}
}
Example: - using Inheritance
using System;
class A
{
int data;
public void f(int arg)
{
data = arg;
}
public int g()
{
return data;
}
}
class B : A
{
public static void Main()
{
B obj2 = new B();
obj2.f(20);
Console.WriteLine(obj2.g());
Console.ReadLine();
}
}