0% found this document useful (0 votes)
2 views4 pages

Example C++ programs

The document provides examples of C++ programming, including the creation of a simple class 'Person' with member functions, a function to find the largest of three numbers, and a structure to manage movie data. It illustrates how to define classes and structures, access their members, and perform basic input/output operations. Each example includes code snippets and expected output.

Uploaded by

georgebrian713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Example C++ programs

The document provides examples of C++ programming, including the creation of a simple class 'Person' with member functions, a function to find the largest of three numbers, and a structure to manage movie data. It illustrates how to define classes and structures, access their members, and perform basic input/output operations. Each example includes code snippets and expected output.

Uploaded by

georgebrian713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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


person1.name = "Alice";
person1.age = 30;

// Call the introduce member method


person1.introduce();

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 << movie.title;
cout << " (" << movie.year << ")\n";
}

You might also like