Question 4 (25 marks) avoid plagiarism. 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.
Question 5 (20
#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;
}
Comments
Leave a comment