“Herencia” Código de respuesta

Herencia

class Print2D
    {
        public int X { get; set; }
        public int Y { get; set; }
        public Print2D(int y,int x)
        {
            X = x;
            Y = y;
            Console.WriteLine("2D");
        }
        public void Printing2D()
        {
            Console.WriteLine("X=\t"+X);
            Console.WriteLine("Y=\t"+Y);
        }
    }
    class Print3D:Print2D
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Z { get; set; }
        public Print3D(int x,int y,int z) : base(x,y)
        {
            Z = z;
            Console.WriteLine("3D");
        }
        public void Printing3D()
        {
            base.Printing2D();
            Console.WriteLine("Z=\t" + Z);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Print3D p3d = new Print3D(4,5,6);
            p3d.Printing3D();
        }
    }
Info Important

herencia

class left {
public:
    void foo();
};

class right {
public:
    void foo();
};

class bottom : public left, public right {
public:
    void foo()
    {
        //base::foo();// ambiguous
        left::foo();
        right::foo();

        // and when foo() is not called for 'this':
        bottom b;
        b.left::foo();  // calls b.foo() from 'left'
        b.right::foo();  // call b.foo() from 'right'
    }
};
Spotless Sloth

herencia

#include <iostream>
 
using namespace std;

// Base class
class Shape {
   public:
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }
      
   protected:
      int width;
      int height;
};

// Derived class
class Rectangle: public Shape {
   public:
      int getArea() { 
         return (width * height); 
      }
};

int main(void) {
   Rectangle Rect;
 
   Rect.setWidth(5);
   Rect.setHeight(7);

   // Print the area of the object.
   cout << "Total area: " << Rect.getArea() << endl;

   return 0;
}
Filthy Flamingo

herencia

// C++ Implementation to show that a derived class
// doesn’t inherit access to private data members.
// However, it does inherit a full parent object.
class A{
  public:
    int x;
  protected:
    int y;
  private:
    int z;
};
class B : public A{
    // x is public
    // y is protected
    // z is not accessible from B
};
class C : protected A{
    // x is protected
    // y is protected
    // z is not accessible from C
}; 
class D : private A{    // 'private' is default for classes
    // x is private
    // y is private
    // z is not accessible from D
};
Sleepy Head

herencia

// Base Class Vehicle
class Vehicle {

    // Private Fields
    private String vehicle;
    private int year;
    private String model;

    // Parameterized Constructor
    public Vehicle(String vehicle, int year, String model) {
        this.vehicle = vehicle;
        this.year = year;
        this.model = model;
    }

    // public method to print details
    public void printDetails() {
        System.out.println("Vehicle Type: " + vehicle);
        System.out.println("Year: " + year);
        System.out.println("Model: " + model);
    }

}

// Derived Class Car
class Car extends Vehicle {

    // Private field

    // Parameterized Constructor
    public Car(String vehicle, int year, String model) {
        super(vehicle, year, model); // calling parent class constructor

    }

    public void carDetails() { // details of car
        printDetails(); // calling method from parent class

    }

}

class Sample {

    public static void main(String[] args) {
        Car elantraSedan = new Car("Car", 2019, "Elantra"); // creation of car Object
        elantraSedan.carDetails(); // calling method to print details
    }
}
Fernandez, Jasmine M.

Respuestas similares a “Herencia”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código