You are on page 1of 2

OOPC

Methods Practice l

#include <iostream>
using namespace std;

class Person {
public:
string firstName;
string lastName;
int age;
string nationality;

void greeting(){
cout<<"Hello, I am "<<firstName<<" "<<lastName<<", "<<age<<" years old
and a "<<nationality<<"!";
}
};

int main(void) {

Person p;

cout<<"Enter first name: ";


cin>>p.firstName;
cout<<"Enter last name: ";
cin>>p.lastName;
cout<<"Enter age: ";
cin>>p.age;
cout<<"Enter nationality: ";
cin>>p.nationality;
cout<<endl;
p.greeting();
return 0;
}

=============================================

Methods Practice 2

#include <iostream>
using namespace std;

class Main{
public:
int num;
void Mighty(){
cout<<"I am mighty!";
}
void displayStrong(){
cout<<"I am "<<num<<" times stronger than you!";
}
};

int main(void) {

Main number;
cout<<"Enter a number: ";
cin>>number.num;
number.Mighty();
cout<<endl;
number.displayStrong();

return 0;
}

==========================================================

Constructor Practice 1

#include <iostream>
using namespace std;

class Phone {
public:
string brand;
int storage;
double price;

};

int main(void) {

Phone cp;
cout<<"Brand: ";
cin>>cp.brand;
cout<<"Storage: ";
cin>>cp.storage;
cout<<"Price: ";
cin>>cp.price;
cout<<endl;
cout<<cp.brand<<" has a memory of "<<cp.storage<<" GB and a price of
Php"<<cp.price<<"!";
return 0;
}

=================================================

Constructors Practice 2

You might also like