Abstract Classes | Sololearn: Learn to code for FREE!
0

Abstract Classes

I'm not sure why its not working. I do exactly what it asks. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Figure rectangle = new Rectangle(5, 6); Figure triangle = new Triangle(4, 8, 3); Console.WriteLine(rectangle.Perimeter()); Console.WriteLine(triangle.Perimeter()); } } abstract class Figure { public abstract int Perimeter(); } class Rectangle : Figure { public int width; public int height; public Rectangle(int width, int height) { this.width = width; this.height = height; } public override int Permieter() { return (width + height) * 2; } } class Triangle : Figure { public int side1; public int side2; public int side3; public Triangle(int s1, int s2, int s3) { this.side1 = s1; this.side2 = s2; this.side3 = s3; } public override int Perimeter() { return (s1 + s2 + s3); } } } This is my code, I'm suppose to add an abstract class and override it in the other classes and add the implementation.

22nd Jun 2022, 12:01 PM
John
3 Answers
+ 2
Hey there, this code worked out for me: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Figure rectangle = new Rectangle(5, 6); Figure triangle = new Triangle(4, 8, 3); Console.WriteLine(rectangle.Perimeter()); Console.WriteLine(triangle.Perimeter()); } } abstract class Figure { //define abstract method Perimeter with no body public abstract int Perimeter(); } class Rectangle : Figure { public int width; public int height; public Rectangle(int width, int height) { this.width = width; this.height = height; } //override Perimeter method for rectangle public override int Perimeter() { return(2*width+2*height); } } class Triangle : Figure { public int side1; public int side2; public int side3; public Triangle(int s1, int s2, int s3) { this.side1 = s1; this.side2 = s2; this.side3 = s3; } //override Perimeter method for triangle public override int Perimeter() { return(side1+side2+side3); } } }
6th Aug 2022, 4:02 AM
Miguel Pineda
0
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { abstract class Shape { public abstract void Draw(); } class Circle : Shape { public override void Draw() { Console.WriteLine("Circle Draw"); } } class Rectangle : Shape { public override void Draw() { Console.WriteLine("Rect Draw"); } } static void Main(string[] args) { Shape c = new Circle(); c.Draw(); } } } Its the same exact thing as the practice? This is pretty bad. Why dont you have any tests made to tell me whats wrong?
22nd Jun 2022, 12:07 PM
John
0
its because of the values being pushed through the paremeters and being stored as a different variable
22nd Jun 2022, 12:17 PM
John