Answer to Question #195433 in C++ for Shema

Question #195433

write a c++ program to create a class called person contain (name, age) as a variables and get and print information as functions?


1
Expert's answer
2021-05-20T00:29:46-0400


#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.
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog