-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic.cpp
140 lines (115 loc) · 4.3 KB
/
Basic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream> // Include the input-output stream library
#include <string> // Include the string library for string manipulation
using namespace std; // Use the standard namespace
// Function to add two integers
int add(int a, int b) {
return a + b; // Return the sum of a and b
}
// Function to greet a user
void greetUser(string name) {
cout << "Hello, " << name << "!" << endl; // Print a greeting message
}
// Function to calculate the factorial of a number (recursive function)
int factorial(int n) {
if (n <= 1) {
return 1; // Base case: factorial of 1 or 0 is 1
} else {
return n * factorial(n - 1); // Recursive case: n * factorial of (n-1)
}
}
// Function to demonstrate basic input and output
void demonstrateIO() {
int age;
string name;
// Prompt the user for their name and age
cout << "Enter your name: ";
cin >> name; // Get the user's name
cout << "Enter your age: ";
cin >> age; // Get the user's age
// Print a message using the provided name and age
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
}
// Base class: Person
class Person {
protected:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {} // Constructor with initialization list
// Virtual function to display information (polymorphism)
virtual void displayInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Derived class: Student (inherits from Person)
class Student : public Person {
private:
string studentID;
public:
Student(string n, int a, string id) : Person(n, a), studentID(id) {} // Constructor
// Override the displayInfo function
void displayInfo() override {
cout << "Name: " << name << ", Age: " << age << ", Student ID: " << studentID << endl;
}
};
// Derived class: Teacher (inherits from Person)
class Teacher : public Person {
private:
string subject;
public:
Teacher(string n, int a, string subj) : Person(n, a), subject(subj) {} // Constructor
// Override the displayInfo function
void displayInfo() override {
cout << "Name: " << name << ", Age: " << age << ", Subject: " << subject << endl;
}
};
// Main function - the starting point of the program
int main() {
// Print a welcome message
cout << "Welcome to the C++ program!" << endl;
// Demonstrate basic input and output
demonstrateIO();
// Demonstrate the add function
int result = add(3, 4); // Call the add function with arguments 3 and 4
cout << "The sum of 3 and 4 is: " << result << endl; // Print the result
// Demonstrate the greetUser function
greetUser("Alice"); // Call the greetUser function with the name "Alice"
// Demonstrate the factorial function
int factResult = factorial(5); // Call the factorial function with the argument 5
cout << "The factorial of 5 is: " << factResult << endl; // Print the result
// Using a for loop to print numbers from 1 to 5
cout << "Numbers from 1 to 5: ";
for (int i = 1; i <= 5; ++i) {
cout << i << " "; // Print the current value of i
}
cout << endl; // Print a new line
// Using a while loop to print numbers from 5 to 1
cout << "Numbers from 5 to 1: ";
int j = 5;
while (j > 0) {
cout << j << " "; // Print the current value of j
--j; // Decrease the value of j by 1
}
cout << endl; // Print a new line
// Using a do-while loop to ensure the loop runs at least once
int num;
do {
cout << "Enter a number (0 to exit): ";
cin >> num; // Get a number from the user
if (num != 0) {
cout << "You entered: " << num << endl; // Print the number
}
} while (num != 0); // Continue the loop until the user enters 0
// Create objects of Person, Student, and Teacher classes
Person p1("John Doe", 30);
Student s1("Jane Smith", 20, "S12345");
Teacher t1("Dr. Brown", 50, "Physics");
// Use polymorphism to display information
Person* people[3] = { &p1, &s1, &t1 };
for (int i = 0; i < 3; ++i) {
people[i]->displayInfo(); // Call the appropriate displayInfo function
}
// End of the program
cout << "Goodbye!" << endl;
return 0; // Return 0 to indicate successful execution
}