finals part one
#include <iostream>
using namespace std;
class Person
{
public:
Person() {count++;}
friend istream& operator>>(istream &,Person &); //operator overlaoding example
friend ostream& operator<<(ostream &, Person &); //operator overloading example
string getName() { return name; }
int getAge() { return age; }
Person &showName(); // this pointer example
Person &showAge(); //this pointer example
static int getCount() { return count; } // static int get function
private:
int age;
string name;
static int count; //static int
};
int Person::count=0; //static int
ostream& operator <<(ostream &output,Person &p)
{
output << "name: "<<p.getName()<<endl<<"age: "<<p.getAge()<<endl;
return output;
}
istream& operator>>(istream &input, Person &p)
{
cout<<"enter age: ";
input >> p.age ;
cout<<"enter name: ";
input >> p.name;
return input;
}
Person &Person::showName()
{
cout<<"using this pointer.."<<endl;
cout<<"name: "<<getName()<<endl;
return *this;
}
Person &Person::showAge()
{
cout<<"age: "<<getAge()<<endl;
return *this;
}
int main()
{
Person p;
cout<<"please enter personal information.."<<endl;
cin>>p;
cout<<"##############################"<<endl;
cout<<p;
cout<<"##############################"<<endl;
p.showName().showAge();
cout<<"##############################"<<endl;
cout<<"how many person has got created by now: "<<Person::getCount()<<endl; // you get directly static int
return 0;
}