Arrays in C++


MyBejs1.h:
class MyBejs1
{
public:
	MyBejs1(void);
	virtual ~MyBejs1(void);
	virtual void printMyString(std::string myString) = 0;
	std::string dawajStringa();
}

MyBejs1.cpp:
std::string MyBejs1::dawajStringa() {
	return myString;
}

MyWeight2.h:
class MyWeight2: public MyBejs1
 {
 public:
     MyWeight2(void);
     MyWeight2(myBejsStruct mbs);
     ~MyWeight2(void);
};

MyWeight2.cpp:
MyBejs1::MyBejs1(void)
{
     myString = "";
}

MyWeight2::MyWeight2(void)
 {
     std::cout << "constructor: MyWeight2::MyWeight2(void)" << std::endl;
 }

std::string MyBejs1::dawajStringa() {
     return myString;
}

main.cpp:
MyWeight2 mw2 = MyWeight2();
mw2.printMyString("huj2");
std::string myStr = mw2.dawajStringa();
std::cout << "myStr: " << myStr.c_str() << std::endl;

Myśle rze takie pszykłady czsza załatfjać w jednym pliku.

Kolejne podejście stont: https://stackoverflow.com/a/9219752/7036047

int rows = 10;
int columns = 15;

//dynamically allocate the multi-dim array in cpp 
int** myDimArray = new int*[rows];  
for(int i = 0; i < rows; i++) {
	myDimArray[i] = new int[columns];  
}

//deallocate the multi-dim array
for(int i = 0; i < rows; i++) {
	delete[] myDimArray[i];  
}
delete[] myDimArray;  

A teras prosta tabela z traj-keczem:

std::array<int, 8> dajSetyIndeksuf(std::vector<RectSize> myRects) {

	std::array<int, 8> myIdxSet = {};
	std::array<int, 5> arr = {3, 4, 5, 1, 2};
	arr[0] = 8;
	arr[1] = 9;

	// takiego areja da sie opsłurzyć w try-catch
	try
	{
		std::cout << arr.at(0);    // No error
		std::cout << arr.at(1);    // No error
		std::cout << arr.at(2);    // No error
		std::cout << arr.at(3);    // No error
		std::cout << arr.at(4) << std::endl;    // No error

		// Throws exception std::out_of_range
		std::cout << arr.at(5) << std::endl;    
	}

	catch (const std::out_of_range&amp; ex)
	{
		std::cerr << ex.what() << std::endl;
	}

	return myIdxSet;
}


Leave a Reply

Your email address will not be published.