write a c++ program to create a class called person contain (name, age) as a variables and get and print information as functions?
#include <iostream>
#include <string>
struct Person {
std::string name;
int age;
};
int main() {
Person a;
a.name = "Calvin";
a.age = 30;
std::cout << a.name << ": " << a.age << std::endl;
}
Print:
#include <iostream>
class Person {
public:
void Print() const;
private:
std::string name_;
int age_ = 5;
};
void Person::Print() const {
std::cout << name_ << ':' << age_ << '\n';
// "name_" and "age_" are the member variables. The "this" keyword is an
// expression whose value is the address of the object for which the member
// was invoked. Its type is "const Person*", because the function is declared
// const.
}
Comments
Leave a comment