1.
Example of Class and Object in C++
// C++ program to illustrate how create a simple class and
// object
#include <iostream>
#include <string>
using namespace std;
// Define a class named 'Person'
class Person {
public:
// Data members
string name;
int age;
// Member function to introduce the person
void introduce()
{
cout << "Hi, my name is " << name << " and I am "
<< age << " years old." << endl;
}
};
int main()
{
// Create an object of the Person class
Person person1;
// accessing data members
[Link] = "Alice";
[Link] = 30;
// Call the introduce member method
[Link]();
return 0;
}
Output : Hi, my name is Alice and I am 30 years old.
2. Finding the largest number
#include <iostream>
using namespace std;
void findLargest(int a, int b, int c)
{
int largest;
if (a >= b && a >= c)
{
largest = a;
}
else if (b >= a && b >= c)
{
largest = b;
}
else
{
largest = c;
}
cout << "The largest number is: " << largest << endl;
}
int main()
{
int num1, num2, num3;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the third number: ";
cin >> num3;
findLargest(num1, num2, num3);
return 0;
}
Data structure.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define N_MOVIES 3
struct movies_t {
string title;
int year;
} films[N_MOVIES];
void printmovie(movies_t movie);
int main() {
string mystr;
int n;
for (n = 0; n < N_MOVIES; n++) {
cout << "Enter title: ";
getline(cin, films[n].title);
cout << "Enter year: ";
getline(cin, mystr);
stringstream(mystr) >> films[n].year;
}
cout << "\nYou have entered these movies:\n";
for (n = 0; n < N_MOVIES; n++)
printmovie(films[n]);
return 0;
}
void printmovie(movies_t movie) {
cout << [Link];
cout << " (" << [Link] << ")\n";
}