USE C++
Demonstrate how to write an interface for a VEHICLE class using C++. In your interface, demonstrate the following (use comments to explain or for demonstration):
-Interface implementation
-Multiple interface implementation for a single class
-Example of a Diamond Inheritance problem
-Polymorphism
Explain each term above clearly in the code example and discuss how it is being used
USE C++
/* Overall problem divided into to 3 section
First program is example of
1) Interface implementation
2) Polymorphism
1st program
*/
#include <iostream>
using namespace std;
// Base class
class Vehicle {
public:
// pure virtual function changeGear
providing interface framework.
virtual void changeGear(int g) =
0;
};
// Derived Bicycle classes that implement all pure virtual method
of class vehicle
class Bicycle: public Vehicle {
int gear;
public:
//override changeGear function
void changeGear(int g) {
gear=g;
cout<<"\n
Bicycle Now the Gear is: "<<gear;
}
};
// Derived Car classes that implement all pure virtual method of
class vehicle
class Car: public Vehicle {
int gear;
public:
//override changeGear function
void changeGear(int g) {
gear=g;
cout<<"\n
Car Now the Gear is: "<<gear;
}
};
int main(void) {
Bicycle Rect;//creat object
Rect.changeGear(1) ;//call override method
Car car;
car.changeGear(3) ;
return 0;
}
/*
output
Bicycle Now the Gear is: 1
Car Now the Gear is: 3
*/
/*
2nd Problem
Example of a Diamond Inheritance problem
The diamond problem occurs when two superclasses of a class have a common base class. For example, in the following diagram, the TA class gets two copies of all attributes of Person class, this causes ambiguities.
*/
#include <iostream>
using namespace std;
// Base class
class Vehicle {
public:
Vehicle(int n){
// only
because of diamond proble below function called twice
cout<<"\n Model No:"<<n;
}
};
// Derived Bicycle classes
class Bicycle: public Vehicle {
int gear;
public:
Bicycle(int n):Vehicle(n){
}
};
// Derived Car classes
class Car: public Vehicle {
int gear;
public:
Car(int n):Vehicle(n){
}
};
class Brand:public Car,public Bicycle{
public:
Brand(int n):Car(n),Bicycle(n){
}
};
int main(void) {
Brand brand(100);
return 0;
}
/*
output
Model No:100
Model No:100
*/
/*
problem 3
Multiple Inheritance in C++
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes.
here is program with vehicle. It generate conflict problem hence it is solved by specifying base class name before function call
*/
#include <iostream>
using namespace std;
class vehicle
{
public:
void someFunction( )
{ cout<<"\n Vehicle Function ";
}
};
class cycle
{
public:
void someFunction( )
{ cout<<"\n cycle Function ";}
};
class derived : public vehicle, public cycle
{
};
int main()
{
derived obj;
obj.vehicle::someFunction( ); //called function
from vehicle class
obj.cycle::someFunction(); // called
function from cycle class
}
/*
output
Vehicle Function
cycle Function
*/
USE C++ Demonstrate how to write an interface for a VEHICLE class using C++. In your...
I could really use the help. Problem Develop a script to demonstrate an understanding of the overload (overwrite) methods of using Python operators. This lesson will focus on the “+” operator and the “__add__” method. The program must have the following: Demonstration of an understanding of how to use the + operator for basic addition of two integer numbers Demonstration of an understanding of how to use the + operator for a concatenation of lists Demonstration of an understanding of...
What is Multiple Inheritance and explain it using a Class Diagram and Write a Code for it in C#. Use Set() and Get() as well.
Write ONE application program by using the following requirements: Using JAVA File input and output Exception handling Inheritance At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods At least one interface: this interface needs to have at least two abstract methods At least one method overloading At least one method overriding At least...
Write a C++ program that does the following : Define a class myInt that has as its single attribute an integer variable and that contains member functions for determining the following information for an object of type myInt: A. Is it multiple of 7 , 11 , or 13. B. Is the sum of its digits odd or even. C. What is the square root value. D.Is it a prime number. E. Is it a perfect number ( The sum...
Q 4(b) 6 Marks] Write the code for a single Java class of your own choice that demonstrates the use of the following features in the Java language: Method overloadingg The use of super and this Identify clearly where in your code each of these points is being demonstrated Q 4(c) [6 Marks] Write an abstract Student class in Java which has a name, id_number, year and programme_code as member variables. Add a constructor to your class and a display()...
C++ ONLY PLEASE Problem Description: Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior Create a Rectangle Class Derive a Square Class from the Rectangle Class Derive a Right Triangle Class from the Rectangle Class Create a Circle Class Create a Sphere Class Create a Prism Class Define any other classes necessary to implement a solution Define a Program Driver Class for Demonstration a) Create a Container to hold objects of the types described above b) Create and Add two(2) objects...
Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...
Programming Message Encoder - Write in Java and include comments Create an interface MessageEncoder that has a single abstract method encode(plainText), where plainText is the message to be encoded. The method will return the encoded message. Create a class SubstitutionCipher that implements the interface MessageEncoder, as described above. The constructor should have one parameter called shift. Define the method encode so that each letter is shifted by the value in shift. Defne the method encode so that each letter is...
#1) If a concrete (non-abstract class is using an interface, it must do which of the following? contain the same methods as the interface inherit the properties of the interface create an interface object all of the above #2) Can multiple catch blocks be executed for a single try statement? Yes No #3) Which of the following is the default access specifier of a class member variable? Private Public Protected Internal #4) Which of the following keyword is used for...
What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...