Write a program in C++ to create a player class. Inherit the classes Cricket_Ilayer, FootbalIJ>]ayer and Hooke}'J)layer from. player class:
Certainly! Below is a sample C++ program that demonstrates the creation of a Player
class and the inheritance of CricketPlayer
, FootballPlayer
, and HockeyPlayer
classes from it:
#include <iostream> #include <string> using namespace std; // Base class class Player { : string name; int age; public: void setName(string n) { name = n; } void setAge(int a) { age = a; } void displayDetails() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; // Derived class 1 class CricketPlayer : public Player { private: int runsScored; public: void setRunsScored(int runs) { runsScore = runs; } void displayCricketDetails() { displayDetails(); cout << "Runs Scored: " << runsScored << endl; } }; // Derived class 2 class FootballPlayer : public Player { private: int goalsScored; public: void setGoalsScored(int goals) { goalsScored = goals } void displayFootballDetails() { displayDetails(); cout << "Goals Scored: " << goalsScored << endl; } }; // Derived class 3 class HockeyPlayer : public Player { private: int goalsMade; public: void setGoalsMade(int goals) { goalsMade = goals; } void displayHockeyDetails() { displayDetails(); cout << "Goals Made: " << goalsMade << endl; } }; int main() { // Creating objects of each player type CricketPlayer cricketPlayer; FootballPlayer footballPlayer; HockeyPlayer hockeyPlayer; // Setting details for each player cricketPlayer.setName("John"); cricketPlayer.setAge(25); cricketPlayer.setRunsScored(100); footballPlayer.setName("David"); footballPlayer.setAge(22); footballPlayer.setGoalsScored(20); hockeyPlayer.setName("Michael"); hockeyPlayer.setAge(28); hockeyPlayer.setGoalsMade(15); // Displaying details for each player cout << "Cricket Player Details:" << endl; cricketPlayer.displayCricketDetails(); cout << endl; cout << "Football Player Details << endl; footballPlayer.displayFootballDetails(); cout << endl; cout << "Hockey Player Details:" << endl; hockeyPlayer.displayHockeyDetails(); cout << endl; return 0; }
In this program, the Player
class serves as the base class, and the CricketPlayer
, FootballPlayer
, and HockeyPlayer
classes inherit from it. Each derived class has additional attributes and methods specific to that type of player. The main
function demonstrates how to create objects of each player type, set their details, and display their information.
Consider a student class with Name, Roll No and Marks as data members. Write a C++ program to implement the member functions get Student Detailso and display Student Detailso. Also write suitable main function.
Certainly! Below is a sample C++ program to implement the Student class with name, roll number, and marks as data members, along with member functions to get and display student details, as well as a suitable main function:
#include <iostream> #include <string> using namespace; class Student { private: string name; int rollNumber; marks; public: void getStudentDetails() { cout << "Enter name: "; getline(cin, name); cout << "Enter roll number: "; cin >> rollNumber; cout << "Enter marks: "; cin >> marks; } void displayStudentDetails() { cout << "Name: " << name << endl; cout << "Roll Number: << rollNumber << endl; cout << "Marks: " << marks << endl; } }; int main() { Student student1; cout << "Enter student details: " << endl; student1.getStudentDetails(); cout << endl << "Student details:" << endl; student1.displayStudentDetails(); return 0; } ` In this program, the `Student` class has data members for name, roll number, and marks, along with member functions `getStudentDetails` to input these details from the user and `displayStudentDetails` to display the entered details. The `main` function creates an object of the `Student` class, gets the student details, and displays them.
write a c++ program that illustrates the concept of function overloading
Certainly! Below is a sample C++ program that demonstrates the concept of function overloading:
#include <iostream> using namespace std; class OverloadingExample { public: // Function with no parameters void display() { cout << "No parameter function called" << endl; } // Function with one integer parameter void display(int a) { cout << "Integer parameter function called with value: " << a << endl; } // Function with two integer parameters void display(int a, int b) { cout << "Two integer parameters function called with values: " << a << " and " << b << endl; } // Function with a double parameter void display(double a) { cout << "Double parameter function called with value: << a << endl; } }; int main() { OverloadingExample example; // Call function with no parameters example.display(); // Call function with one integer parameter example.display(5); // Call function with two integer parameters example.display(10, 20); // Call function with a double parameter example.display(3.14); return 0; }
In this program, the OverloadingExample
class contains multiple display
functions with different parameter lists. This demonstrates function overloading, where functions with the same name but different parameters can be defined within a class. In the main
function, we call each of the overloaded display
functions to illustrate how the appropriate function is chosen based on the arguments provided.
W;ite a program in C++ to imple;Gent database of persons having different profession such as engineer, doctor, and student using the c(Inc>cpt of multiple inherltance'
Certainly! Below is a C++ program that demonstrates the implementation of a database of persons with different professions (engineer, doctor, and student) using the concept of multiple inheritance:
#include <iostream> #include <string> using namespace std; // Base class for Person class Person { protected: string name; public: void setName(string n) { name = n; } }; // Engineer class derived from Personclass Engineer : public Person { protected: string engineeringField; public: void setEngineeringField(string field) { engineeringField = field; } void displayEngineerDetails() { cout << "Name: " << name << endl; cout << "Engineering Field: " << engineeringField << endl; } }; // Doctor class derived from Person class Doctor : public Person { protected: string specialization; public: void setSpecialization(string spec) { specialization = spec; } void displayDoctorDetails() { cout << "Name: " << name << endl; cout << "Specialization: " << specialization << endl; } }; // Student class derived from Person class Student : public Person { protected: string course; public: void setCourse(string c) { course = c; } void displayStudentDetails() { cout << "Name: " << name << endl; cout << "Course: " << course << endl; } }; // Database class using multiple inheritance class PersonDatabase : public Engineer public Doctor, public Student { public: void displayPersonDetails() { cout << "Person Details:" << endl; cout << "-----------------" << endl; displayEngineerDetails(); displayDoctorDetails(); displayStudentDetails(); } }; int main() { PersonDatabase person; // Setting details for an engineer person.setName("John Doe"); person.setEngineeringField("Software Engineering"); // Setting details for a doctor person.setName("Dr. Sarah Smith"); person.setSpecialization("Cardiology"); // Setting details for a student person.setName("Alice Johnson"); person.setCourse("Computer Science"); // Displaying details for all persons person.displayPersonDetails(); return 0; }
In this program, the Person
class serves as the base class, and the Engineer
, Doctor
, and Student
classes are derived from it. The PersonDatabase
class demonstrates multiple inheritance, inheriting from Engineer
, Doctor
, and Student
classes. Each derived class has its specific details and display functions. In the main
function, details are set for an engineer, a doctor, and a student, and then their details are displayed using the PersonDatabase
class.
Cr;ate a-cl;ss complex to represent complex number with overloaded multiD]icatiOn operators. Use them in a main program
Here's a C++ program that defines a Complex
class represent complex numbers and overloads the multiplication operator to perform multiplication complex numbers. The program also demonstrates the use of the overloaded multiplication operators in a main:
#include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(double r = 0, i = 0) : real(r), imag(i) {} // Overloaded multiplication operator to multiply two complex numbers operator*(const Complex& c) { Complex temp; temp.real = (real * c.real) - (imag * c.imag temp.imag = (real * c.imag) + (imag c.real); return temp; } void display() { if (imag < 0) { cout << "Complex Number " << real << " - " << -imag << "i" << endl; } else { << "Complex Number: " << real << " + " << imag << "i" << endl; } } }; int main() { Complex num1(3, 2); Complex num2(1,4); // Display original complex numbers cout << "Original Complex Numbers:" << endl; num1.display(); num2.display(); // Multiply the complex numbers using the overloaded multiplication operator Complex result = num1 * num2; // Display the result cout << "Result of Multiplication:" << endl result.display(); return 0; }
In this program, the Complex
class represents a complex number with real and imaginary parts. The *
operator is overloaded in the Complex
class to perform multiplication of complex numbers. In the main
function, two complex numbers are created, and their multiplication is performed using the overloaded *
operator. The result of the multiplication is then displayed.