19.7k views
5 votes
C++ code pls write the code

C++ code pls write the code-example-1
User Krxldfx
by
5.8k points

1 Answer

7 votes

Answer:

Following are the code to this question:

#include<iostream>//defining header file

using namespace std;

class vec//defining class vec

{

int x, y, z; //defining integer varaible x, y, z

public: // using public access specifier

vec() //defining default constructor

{

x = 0; //assign value 0 in x variable

y = 0; //assign value 0 in y variable

z = 0;//assign value 0 in z variable

}

vec(int a, int b , int c) //defining parameterized constructor vec

{

x = a; //assign value a in x variable

y = b; //assign value b in y variable

z = c;//assign value c in z variable

}

void printVec() //defining a method printVec

{

cout<<x<<","<<y<<","<<z<<endl; //use print method to print integer variable value

}

//code

vec& operator=(const vec& ob) //defining operator method that create object "ob" in parameter

{

x=ob.x; //use ob to hold x variable value

y=ob.y;//use ob to hold y variable value

z=ob.z;//use ob to hold z variable value

return *this;//using this keyword to return value

}

};

int main()//defining main method

{

vec v(1,2,3);//calling parameterized constructor

vec v2; //creating class object to call method

v2=v; //creatring reference of object

v2.printVec(); //call method printVec

return 0;

}

Output:

1, 2, 3

Step-by-step explanation:

In the above-given code part, a method "operator" is defined, that accepts an object or we can say that it is a reference of the class "ob", which is a constant type. Inside the method, the object is used to store the integer variable value in its variable and use the return keyword with this point keyword to return its value.

User Srakl
by
5.5k points