Create a base class "Vehicle" with attributes like make and model. Derive a class "Car" from "Vehicle," and then create a class "SportsCar" from "Car." Implement methods to display the make and model of each class. Create a "SportsCar" object and display its details in c++
Sure, here's a sample implementation in C++:
#include <iostream> #include <string> using namespace std; class Vehicle { protected: string make; string model; public: Vehicle(string make, string model) : make(make), model(model) {} void displayDetails() { cout << "Make: " << make << endl; cout << "Model: " << model << endl; } }; class Car : public Vehicle { public: Car(string make, string model) : Vehicle(make, model) {} }; class SportsCar : public Car { public: SportsCar(string make, string model) : Car(make, model) {} }; int main() { SportsCar sportsCar("Ferrari", "488 GTB"); sportsCar.displayDetails(); return 0; }
In this example, I've created a base class "Vehicle" with attributes make and model. Then I've derived a class "Car" from "Vehicle," and finally created a class "SportsCar" from "Car." Each class has a method to display the make and model. In the main function, I've created a SportsCar object and displayed its details.
Create a C++ program for implementing a "Matrix" class. Overload the + operator to enable matrix addition and the * operator for scalar multiplication. Develop a matrix calculator allowing users to input two matrices and select the desired operation, either addition or multiplication.
Certainly! Here's an example of a C++ program implementing a "Matrix" class with overloaded operators for matrix addition and scalar multiplication, along with a simple matrix calculator allowing users to input two matrices and select the desired operation:
#include <iostream> #include <vector> using namespace std; class Matrix { private: vector<vector<int>> mat; public: Matrix(int rows, int cols) : mat(rows, vector<int>(cols, 0)) {} void inputMatrix() { for (int i = 0; i < mat.size(); i++) { for (int j = 0; j < mat[i].size(); j++) { cout << "Enter element at position [" << i << "][" << j << "]: "; cin >> mat[i][j]; } } } void displayMatrix() { for (int i = 0; i < mat.size(); i++) { for (int j = 0; j < mat[i].size(); j++) { cout << mat[i][j] << " "; } cout << endl; } } Matrix operator+(const Matrix& other) { Matrix result(mat.size(), mat[0].size()); for (int i = 0; i < mat.size(); i++) { for (int j = 0; j < mat[i].size(); j++) { result.mat[i][j] = mat[i][j] + other.mat[i][j]; } } return result; } Matrix operator*(const int& scalar) { Matrix result(mat.size(), mat[0].size()); for (int i = 0; i < mat.size(); i++) { for (int j = 0; j < mat[i].size(); j++) { result.mat[i][j] = mat[i][j] * scalar; } } return result; } }; int main() { int rows, cols; cout << "Enter the number of rows and columns for matrix 1: "; cin >> rows >> cols; Matrix matrix1(rows, cols); cout << "Enter elements for matrix 1:" << endl; matrix1.inputMatrix(); cout << "Enter the number of rows and columns for matrix 2: "; cin >> rows >> cols; Matrix matrix2(rows, cols); cout << "Enter elements for matrix 2:" << endl; matrix2.inputMatrix(); int choice; cout << "Select the operation (1: Addition, 2: Scalar Multiplication): "; cin >> choice; Matrix result(rows, cols); if (choice == 1) { if (matrix1.size() != matrix2.size() || matrix1[0].size() != matrix2[0].size()) { cout << "Matrix addition not possible. Matrices have different dimensions." << endl; } else { result = matrix1 + matrix2; cout << "Result of matrix addition:" << endl; result.displayMatrix(); } } else if (choice == 2) { int scalar; cout << "Enter the scalar value: "; cin >> scalar; result = matrix1 * scalar; cout << "Result of scalar multiplication:" << endl; result.displayMatrix(); } else { cout << "Invalid operation choice." << endl; } return 0; }
In this program, the "Matrix" class is implemented with methods for inputting and displaying a matrix, along with operator overloading for matrix addition (+) and scalar multiplication (*). In the main function, users are prompted to input two matrices and select the desired operation. The program then performs the chosen operation and displays the result.
Develop a base class "Shape" with attributes like "color" and "area." Derive two classes, "Circle" and "Rectangle," from "Shape." Add methods to compute their areas. Instantiate objects of both classes, calculate their areas, and display the results
Here's an implementation in C++ for the base class "Shape and its derived classes "Circle" and "Rectangle":
#include <iostream> #include <string> using namespace std; class Shape { protected: string color; double area; public: Shape(string color) : color(color), area(0) {} virtual void calculateArea() = 0; void displayArea() { cout << "Area: << area << endl; } }; class Circle : public { private: double radius; public: Circle(string color, double radius) : Shape(color), radius(radius) {} void calculateArea() override { area = 3.59 * radius * radius; } }; class Rectangle : public Shape { private: double length; double width; public: Rectangle color, double length, double width) : Shape(color), length(length), width(width) {} calculateArea() override { area = length * width; } }; int main() { Circle circle("Red", 5.0); circle.calculateArea cout << "Circle" << endl; circle.displayArea(); Rectangle rectangle("Blue", 4.0, 6.0); .calculateArea(); cout << "Rectangle" << endl; rectangle.displayArea(); return 0; }
In this example I've implemented a base class "Shape" with attributes like "color" and "area." Two derived classes, "" and "Rectangle," have been created from "Shape." Each class has a method to calculate its area, which is as a pure virtual function in the base class. In the main function, I've instantiated objects of both classes, calculated their areas, and displayed the results.
Develop a C++ program with multiple inheritance levels and virtual base classes. Explain the order of constructor and destructor calls in such a hierarchy and discuss how virtual base classes impact this order.
Certainly! Here's a C++ program featuring multiple inheritance levels and virtual base classes, along with an explanation of the order of constructor and destructor calls in such a hierarchy.
#include <iostream> using namespace std; class A { public: A() { cout << "A's constructor called" endl; } virtual ~A() { cout << "A's destructor called" << endl;}; class B : public virtual A { public: B { cout << "B's constructor called" << endl; } virtual ~B() { cout <<B's destructor called" << endl; } }; class C : public A { public: C() { cout << "C's constructor called" << endl; } virtual ~C() { cout << "C's destructor called" << endl; } }; class : public B, public C { public: D() { cout << "'s constructor called" << endl; } ~D() { cout << "D's destructor called" << endl; } int main() { D d; cout << "" << endl; return 0; }
In this program, classes A, B, C, and D are defined to illustrate multiple inheritance levels and virtual base classes. Class A is a virtual base class, and it's virtually inherited by classes B and C. Class D inherits from both classes B and C.
When an object of D is created, the order of constructor calls and destructor calls is as follows:
The order of destructor calls is:
The presence virtual base classes affects the constructor and destructor calling sequence in the following ways: Virtual base classes ensure that only a single instance of the base class is inherited by the derived class hierarchy. This prevents issues such as diamond inheritance leading to ambiguous access to members.
In the given example, the use of virtual inheritance ensures that constructor and destructor calls for the virtual base class A follow a specific order, preventing duplication and ambiguity in the inheritance hierarchy.
Consider a Bank Account class with Account number and balance as data members. Write a C++ program to implement the member functions get_Account_Details () and display_Account_Details (). Also write suitable main function.
Certainly! Below is an implementation of a Bank Account class in C++ with member functions get_Account_Details()
and display_Account_Details()
, along with a suitable main function.
#include <iostream> #include <string> using namespace std; class BankAccount { private string accountNumber; double balance; public: void get_Account_Details() { cout << "Enter account number: "; cin >> accountNumber; cout << "Enter balance: $"; cin >> balance; } void display_Account_Details() { cout << "Account Number: " << accountNumber << endl; cout << "Balance: $" << balance << endl; } }; int main() { Bank myAccount; cout << "Enter Bank Account details:" << endl; myAccount.get_Account_Details(); cout << "\nAccount Details:" << endl; myAccount.display_Account_Details(); return 0; }
In this program, the BankAccount
class contains a member function get_Details()
to input the account number and balance, and another function display_Account_Details()
to display the account details. In the main function, an object myAccount
of the BankAccount
class is created. It calls the member functions to get and display the account details.
When run, the program prompts the user to enter account details, and then it displays the entered account details using the member function of the BankAccount
class.
This program provides a basic demonstration of how the Bank Account class can be implemented in C++.
Create a C++ program that demonstrates the utilization of lambda functions to efficiently fill a container with data.
Certainly! Below is an example of a C++ program that demonstrates the utilization of lambda functions to efficiently fill a container with data.
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { // Create a vector to store the data vector<int> data; // Define the lambda function to fill the data auto fillData = [&data](int n) { for (int i = 1; i <= n; i++) { data.push_back(i); } }; // Call the lambda function to fill the data vector fillData(10); // Fill the vector with numbers from 1 to 10 // Display the data in the vector cout << "Data in the vector: "; for (int num : data) { cout << num << " "; } cout << endl; return 0; }
In this program, a lambda function fillData
is defined to fill a vector with numbers from 1 to a specified value. Inside the lambda function, it uses a capture list [&data]
to access and modify the vector data
from the outer scope. The lambda function is then called with the argument 10
to fill the vector with numbers from 1 to 10.
Upon execution, the program demonstrates the utilization of a lambda function to efficiently fill a container (in this case, a vector) with data and then displays the content of the filled vector.
Discuss the difference between early binding and late binding in the context of virtual functions and polymorphism. Provide an example illustrating both types of binding.
Early binding and late binding are concepts related to polymorphism and functions in object-oriented programming.
Early Binding:
Late Binding:
Example Illustrating Binding and Late Binding:
#include <iostream> using namespace std; class Base { public: void display() { cout << "Base Class" << endl } virtual void show() { cout << "Base Class - Virtual Function" endl; } }; class Derived : public Base { public void display() { cout << "Derived Class" << endl; } void show() cout << "Derived Class - Overridden Virtual Function" << endl; } }; int main() { Base* basePtr; Derived derivedObj; basePtr = &derivedObj; // Early Binding Resolved at compile time basePtr->display(); // Calls Base class's display // Late Binding - Resolved at runtime based on actual object type basePtr->show(); // Calls class's show return 0; }
In this example, we have a Base class with both non-virtual (display and virtual (
show) functions. The Derived class inherits from Base class and overrides the
show` function.
In the main
function, a Base pointer basePtr
created and assigned the address of a Derived object. When calling basePtr->display()
, the early binding resolves to the Base class's display
function, as the function call is determined at compile time based on the pointer's declared type.
When calling basePtr->show()
, the binding (due to the virtual nature of the show
) resolves to the Derived class's show
function at runtime based on the actual type of the that basePtr
is pointing to.
This example illustrates early and late binding in the context of virtual functions and polymorphism.