Professional Documents
Culture Documents
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;
=============================================
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