passing a variable and its value from one class to another through inheritance c#

44 views Asked by At

I want to use inheritance when calculating the area and volume of a cone. I have a circle class whose variables I want to use. I don’t understand why the area value is not transferred and calculated from the circle class to the conuse class. с#

 class Program
    {

        static void Main()
        {
            Console.WriteLine("Выберите фигуру  и введите цифру:\n1. Квадрат\n2. Прямоугольник\n3. Круг\n4. Треугольник\n5. Конус\n6. Пирамида");
            int figure = Convert.ToInt32(Console.ReadLine());
            var fig = new Figure();
            //fig.peredacha_znacheniya(figure);
            switch (figure)
            {
                case 1:

                    var square = new Square();
                    square.Info();
                    square.Stchet();
                    square.Vyvod();
                    //side = Square  
                    break;

                case 2:

                    Rectangle rectangle = new Rectangle();
                    rectangle.Info();
                    rectangle.Stchet();
                    rectangle.Vyvod();
                    break;
                case 3:

                    Circle circle = new Circle();
                    Console.WriteLine("Введите радиус круга");
                    double radius = Convert.ToInt32(Console.ReadLine());
                    circle.Info_circle();
                    circle.Stchet();
                    circle.Vyvod();
                    break;
                case 4:

                    Triangle triangle = new Triangle();

                    triangle.Info();
                    triangle.vid();
                    triangle.Stchet();
                    //triangle.vid();
                    triangle.Vyvod();

                    break;
                case 5:

                    Conus conus = new Conus();
                    Circle circle_for_conus = new Circle();
                    //circle_for_conus.Info();

                    conus.Info_circle();
                    conus.Stchet();
                    
                    conus.Info_conus();
                   
                    conus.Stchet();
                    conus.Vyvod_conus();
                    break;
                case 6:
                   
                    string vid_piramidy;
                    Console.WriteLine("Введите 'квадрат', если хотите квадратную пирамиду\n" +
                        "Введите 'прямоугольник', если хотите прямоугольную пирамиду\n");
                    vid_piramidy = Console.ReadLine();
                    var vid_strWithoutSpaces = vid_piramidy.Replace(" ", "");
                    if (vid_strWithoutSpaces.ToLower() == "квадрат")
                    {
                        Console.WriteLine(vid_strWithoutSpaces);
                    }
                    if (vid_strWithoutSpaces.ToLower() == "прямоугольник")
                    {
                        Console.WriteLine(vid_strWithoutSpaces);
                    }
                    break;
                    //    Triangle triangle = new Triangle();
                    //    break;
                    //case 6:
                    //    Conus conus = new Conus();
                    //    break;

            }
            
        }
    }

class circle

 class Circle : Figure
    {
        protected double radius;
        protected double C;/*{ get; set; }*/
        protected double S; /*{ get; set; }*/
        public virtual void Info_circle()
        {
            Console.WriteLine("Введите радиус круга");
            this.radius = Convert.ToInt32(Console.ReadLine());
            return;
        }
        //public Circle(double radius, double s)
        //{
        //    this.radius = radius;

        //    this.S = s;
        //}

        public virtual void Stchet()

        {

            if (radius > 0)
            {
                this.S = Math.PI * Math.Pow(radius, 2);
                this.C = radius * 2 * Math.PI;
            }

            else
            {
                Console.WriteLine("Существование такой окружности невозможно, это точка в пространстве");
            }
            return;


        }
        //public  Circle()
        //{

        //    S = this.S;
        //    radius = this.radius;

        //}
        //public double AA
        //{
        //    get { return S; }
        //    set { S = value; }  
        //} 
        public void Vyvod()
        {
            Console.WriteLine($"Периметр окружности: {C} \n Площадь окружности: {S}\n");
        }
    }

}

class conus (cone)

 class Conus : Circle
    {
        protected double h;
        protected double V;
        protected double S_conusa;
        
       
        public void Info_conus()
        {
            Console.WriteLine("Введите высоту конуса");
            this.h = Convert.ToDouble(Console.ReadLine());
        }
        public override void Stchet(/*double radius, double S*/)/*: base(radius, S)*/
        {
            //V=1/3* S ocnovania * h

            this.V = 1/3 * base.S * this.h;
            this.S_conusa = Math.PI * base.radius * Math.Sqrt(Math.Pow(base.radius, 2) + Math.Pow(this.h, 2)) + base.S;
            
        }

        //public Conus() : base()
        //{

        //}
        //public override Conus( double S, double radius) : base (S, radius)
        //{

        //}

        //public void conus(Circle circle)
        //{
        //    circle.Stchet();
        //}
        public void Vyvod_conus()
        {
            Console.WriteLine($"Площадь конуса равна {S_conusa}, объём равен {this.V}");
        }
        
    }
    //{ this.h = h; }
}

Using the constructor doesn't work for some reason, or I'm doing something wrong. I need inheritance to make this all work, I don't understand what the problem is

0

There are 0 answers