Advertisement
alansam

Basic class

Apr 24th, 2022 (edited)
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std::literals::string_literals;
  7.  
  8. class rollno {
  9. private:
  10.   std::string name_;
  11.   uint32_t nr_;
  12.  
  13. public:
  14.   rollno(std::string const & name = ""s, uint32_t nr = 0u) : name_ { name }, nr_ { nr } {}
  15.   rollno(rollno const & that) = default;
  16.   rollno(rollno && that) = default;
  17.   ~rollno() = default;
  18.   rollno & operator=(rollno const & that) = default;
  19.   rollno & operator=(rollno && that) = default;
  20.  
  21.   std::string name(void) const { return name_; }
  22.   std::string name(std::string const & name) { name_ = name; return name_; }
  23.   unsigned nr(void) const { return nr_; }
  24.   unsigned nr(uint32_t nr) { nr_ = nr; return nr_; }
  25. };
  26.  
  27. int main() {
  28.   auto roll = std::vector<rollno> {
  29.     { "Rohit"s, 3u, },
  30.     { "Priya"s, 4u, },
  31.     { "Sneha"s, 5u, },
  32.   };
  33.  
  34.   auto rt1 = rollno("Nish"s, 10u);
  35.   roll.push_back(rt1);
  36.   rt1.name("Mehrdad"s);
  37.   rt1.nr(11u);
  38.   roll.push_back(rt1);
  39.  
  40.   std::cout << "Student rollcall is:\n"s;
  41.   for (rollno & rn : roll) {
  42.     std::cout << std::setw(10) << rn.name()
  43.               << std::setw(3) << rn.nr()
  44.               << '\n';
  45.   }
  46.  
  47.   return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement