473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Creating an array using class in C++

yue219
7
Im having trouble with this one. I need to create a grid using class, but not sure how to write the code. Please i just need hints =]


Expand|Select|Wrap|Line Numbers
  1.  
  2.         //array size for Template
  3.         const int R = 4; 
  4.         const int C = 3;
  5.  
  6.     //This is what the player sees upon starting the game.
  7.     char Template[R][C];
  8.     Template[0][0] = 'X';
  9.     Template[0][1] = 'X';
  10.     Template[0][2] = 'X';
  11.     Template[1][0] = 'X';
  12.     Template[1][1] = 'X';
  13.     Template[1][2] = 'X';
  14.     Template[2][0] = 'X';
  15.     Template[2][1] = 'X';
  16.     Template[2][2] = 'X';
  17.     Template[3][0] = 'X';
  18.     Template[3][1] = 'X';
  19.     Template[3][2] = 'X';
  20.  
Oct 18 '07 #1
2 1842
Im having trouble with this one. I need to create a grid using class, but not sure how to write the code. Please i just need hints =]
Hello yue219!

You need something like this.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. //-------------------------------------------------------   
  3. class cGrid
  4. {
  5.   public:
  6.      cGrid(int _r, int _c)   //Constructor
  7.        :R(_r),C(_c)//Initialize dimensions
  8.      {
  9.         Template=new char*[R];
  10.         for (int i=0; i<R; ++i)
  11.         {
  12.           Template[i]=new char[C];
  13.           for (int j=0; j<C; ++j)
  14.               Template[i][j]='X';
  15.         } //Initializing array
  16.      }
  17.      ~cGrid() //Destructor
  18.      {
  19.         for (int i=0; i<R; ++i) delete[] Template[i];
  20.         delete []Template; //Release heap memory   
  21.     }
  22.      int R;
  23.      int C;
  24.      char** Template;
  25. };
  26. //---------------------------------------------------------------------------
  27. int main(int argc, char* argv[])
  28. {
  29.     cGrid myGrid(4,3);
  30.     //You have an instance of cGrid, that contains three fields
  31.     //int R - number of rows
  32.     //int C- number of columns
  33.     //char** Template - 2d array (R*C) each element of which equals 'X'
  34.     //-----------------------------------------------------------------
  35.     //To reach Template's elements use this code
  36.     //myGrid.Template[i][j]
  37.    //Code compiled in C++ Builder.
  38. }
  39.  
That's all. Good luck!
Oct 18 '07 #2
yue219
7
Hello yue219!

You need something like this.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. //-------------------------------------------------------   
  3. class cGrid
  4. {
  5.   public:
  6.      cGrid(int _r, int _c)   //Constructor
  7.        :R(_r),C(_c)//Initialize dimensions
  8.      {
  9.         Template=new char*[R];
  10.         for (int i=0; i<R; ++i)
  11.         {
  12.           Template[i]=new char[C];
  13.           for (int j=0; j<C; ++j)
  14.               Template[i][j]='X';
  15.         } //Initializing array
  16.      }
  17.      ~cGrid() //Destructor
  18.      {
  19.         for (int i=0; i<R; ++i) delete[] Template[i];
  20.         delete []Template; //Release heap memory   
  21.     }
  22.      int R;
  23.      int C;
  24.      char** Template;
  25. };
  26. //---------------------------------------------------------------------------
  27. int main(int argc, char* argv[])
  28. {
  29.     cGrid myGrid(4,3);
  30.     //You have an instance of cGrid, that contains three fields
  31.     //int R - number of rows
  32.     //int C- number of columns
  33.     //char** Template - 2d array (R*C) each element of which equals 'X'
  34.     //-----------------------------------------------------------------
  35.     //To reach Template's elements use this code
  36.     //myGrid.Template[i][j]
  37.    //Code compiled in C++ Builder.
  38. }
  39.  
That's all. Good luck!
Ohh thank you mucho =]
Oct 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: cody | last post by:
i basically want to create an object which contains an array (the last element of the class). the size of the array is determined when the object is created. for performance reasons (avoiding...
6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
3
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
3
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown below and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
7
by: anupamsps | last post by:
HI all, let me explain the problem: In my simulation I am using two class: particle and Container. //////////// the outline of particle class/////////////////////...
6
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the...
11
by: Matthew Wells | last post by:
Hello. I have figured out how to create an instance of an object only knowing the type by string. string sName = "MyClassName"; Type t = Type.GetType(sName); Object objNew =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.