Answer to Question #282539 in C++ for Bismark Akyen

Question #282539

Write the following program in c++. i. Create a class of your own choice with 5 attributes. ii. Create an object of named objone in the intmain() function iii. Access attributes and set values for all the 5 attributes iv. Print all the attribute values v. Create a function in the class that add two number and access the function using the oblject.


1
Expert's answer
2021-12-27T15:52:47-0500
#include <iostream>
using namespace std;
class Cls{
  public:
  int attr1;
  int attr2;
  int attr3;
  int attr4;
  int attr5;


  int addtwo(int one, int two){
    return one + two;
  }


};


int main(){
  Cls objone;
  objone.attr1 = 1;
  objone.attr2 = 2;
  objone.attr3 = 3;
  objone.attr4 = 4;
  objone.attr5 = 5;


  cout<<"attr1 = "<<objone.attr1;
  cout<<"\nattr2 = "<<objone.attr2;
  cout<<"\nattr3 = "<<objone.attr3;
  cout<<"\nattr4 = "<<objone.attr4;
  cout<<"\nattr5 = "<<objone.attr5;
  cout<<"\nattr1 + attr2 = "<<objone.addtwo(objone.attr1, objone.attr2)<<endl;
  return 0;
}

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