Learning C++ for the first time. Feedback? - Raspberry Pi Forums


ok, i've started learning c++ first time. used fair java programmer 16 years ago have done little programming since.

i'm following tutorials found on net , had go @ operator overloading , i'd little bit of feedback on how tidy code bit. it's simplistic @ moment , done purely example myself on how it. i'd know if there's best practice way things , whether i've done breaks rules ( though compiles , works ). code below along program output.

in advance.

code: select all

//test.hpp #ifndef tc #define tc  class testclass { 	private: 		int value; 	 	public: 		testclass (); 		testclass (int x); 		 		void setvalue (int x); 		int getvalue (); };  testclass operator*(testclass lhs, testclass rhs);  #endif  //test.cpp #include "test.hpp"  testclass::testclass(){ 	value=0; }  testclass::testclass(int x){ 	value=x; }  void testclass::setvalue(int x){ 	value=x; }  int testclass::getvalue(){ 	return value; }  testclass operator*(testclass lhs, testclass rhs){ 	testclass rv ( lhs.getvalue()*rhs.getvalue()); 	return rv; }  //tcmain.cpp #include <iostream> #include "test.hpp"  using namespace std;  int main(){ 	 	testclass tc; 	 	cout<<"the value of tc is: "<<tc.getvalue()<<"\n"; 	 	tc.setvalue(10); 	 	cout<<"the value of tc is: "<<tc.getvalue()<<"\n"; 	 	testclass tc2(25); 	 	cout<<"the value of tc2 is: "<<tc2.getvalue()<<"\n"; 	 	testclass tc3= tc*tc2; 	 	cout<<"the value of tc3 is: "<<tc3.getvalue()<<"\n"; 	 	return 0; 	 }   program output: value of tc is: 0 value of tc is: 10 value of tc2 is: 25 value of tc3 is: 250  

looks fine. if operator friend of class make more efficient accessing value directly rather through getvalue function.


raspberrypi



Comments