473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

doubt on abstration concept

hi friends,
i have placed code because i want to explain my doubt clearly to
you ,and i have placed my doubt in Main of the program (static void
Main(string[] args)) by // before that

using System;

namespace AbstractClasses
{
abstract class Shape
{
public abstract double GetArea();
public abstract double GetPerimeter();
}
class Circle : Shape
{
public double Radius;
public override double GetArea()
{
return Math.PI * Radius * Radius;
}
public override double GetPerimeter()
{
return 2 * Math.PI * Radius;
}
}
class Rectangle : Shape
{
public double Width, Height;
public override double GetArea()
{
return Width * Height;
}
public override double GetPerimeter()
{
return 2 * (Width + Height);
}
}
class Class1
{
static void Main(string[] args)
{
Shape s = new Shape();
Shape [] shapes = new Shape[2];
Circle c = new Circle();
c.Radius=5;
Rectangle r = new Rectangle();
r.Width=8;
r.Height=6;
shapes[0]=c;//what we can achiev by assigning object c to

shapes reference ,is there any special use in

having abstract class like this [donot consider

this example alone]
shapes[1]=r;
double total = 0;
foreach (Shape s in shapes)
total += s.GetArea();
Console.WriteLine("The total area is {0}", total);
}
}
}

Sep 17 '07 #1
2 1192
Hi,

"rajendra" <kr******@gmail.comwrote in message
news:11*********************@n39g2000hsh.googlegro ups.com...
shapes[0]=c;//what we can achiev by assigning object c to

shapes reference ,is there any special use in

having abstract class like this [donot consider

this example alone]
shapes[1]=r;
double total = 0;
foreach (Shape s in shapes)
total += s.GetArea();
Console.WriteLine("The total area is {0}", total);
Well you example exemplify that clearly. You can calculate the area of any
figure you have define without knowing the real type of the figure, why?
because you know that all figures will implement the methods that your base
class (Shape) defined.
But in the same hand you do not want an instance of Shape created.
Sep 17 '07 #2
On Sep 17, 8:03 am, rajendra <krksh...@gmail.comwrote:
what we can achiev by assigning object c to
shapes reference ,is there any special use in
having abstract class like this [donot consider
this example alone]
Like Ignacio said that was the perfect example. But other examples
include plugin architectures where you can treat different plugins as
being similar enough to get them initialized and executing.

You can also get this behavior by using interfaces. Consider an
expression evaluator.

Compiler compiler = new Compiler();
IExpression expression = compiler.Compile("sin(3) * log(5)");
Console.WriteLine(expression.Evaluate());

In the above example the Compile method just returns an object that
implements IExpression. The actual concrete type it returns could be
anything. It could be an AddExpression, or SinExpression, or some
other type that can represent any arbitrarily complex expression. The
neat thing about it is that *all* expressions are treated the same
way. That is, they all have the Evaluate method and can thus be
evaluated without knowing the actual type.

Sep 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
1
by: muthu | last post by:
Hi freinds, Iam using repeator control in my application.The code behind is vb.net. I have n number of rows and 5 columns in the repeator control.In the ItemDataBound event of the repeator...
13
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top ...
12
by: Shhnwz.a | last post by:
Hi, I want to know some of the situations , when to use memcpy() and when to memset(); Thanx in Advance..
122
by: ivan | last post by:
hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks
0
by: Balasubramani | last post by:
class A { public void getID(A a){ } } class B : A {
1
by: sam_cit | last post by:
Hi Everyone, I searched for mmap() and i found the following in wikipedia, 'Anonymous mappings are mappings of physical RAM to virtual memory. This is similar to malloc, and is used in some...
1
rajiv07
by: rajiv07 | last post by:
Hi to all i cannot understand the concept of tie function. is anybody explain to me pelase reply thank u.
11
by: subramanian100in | last post by:
In http://www.parashift.com/c++-faq-lite/friends.html#faq-14.5 the following is mentioned: (member functions don't allow promotion of the left hand argument, since that would change the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.