Author Topic: Guidance for writing C++ code  (Read 7326 times)

0 Members and 1 Guest are viewing this topic.

Offline ajingi

  • Sr. Member
  • ****
  • Join Date: Feb 2004
  • Location: North
  • Posts: 272
    • View Profile
Guidance for writing C++ code
« on: February 21, 2004, 03:53:57 PM »
Writting programming code varies from programme to programme. To make it easier for us to read each other's code, here are a few guidelines to follow when writing C++ code.

Definitions
?Member functions are functions that are associated with ?classes.
?Data members are variables that are part of classes.

Identifier Names
?All variable and constant identifiers begin with a lowercase letter.
?Global variables begin with the (for example, theWindows).
?Constants begin with lowercase c (for example, cMaxWindows).
?Class data members begin with its (for example, itsCanvas).
?Static data members begin with their (for example, theirTotal).
?Both member and non-member functions begin with an uppercase letter (for example, TextStyle).
?Names of classes begin with an uppercase letter (for example, Canvas).
I object to violence because when it appears to do good, the good is only temporary; the evil it does is permanent.

Offline zezezee

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Location: Kano
  • Posts: 372
    • View Profile
Re: Guidance for writing C++ code
« Reply #1 on: February 22, 2004, 05:29:47 AM »
This is very educative man.....keep da good things comin homies...hey man, just wonderin if u cld drop a sub or function and identify the main components of the sub or function. include an object, mayb a text box and a class if possible....thanx alot..
 just realised that nothing is what it seems.

Offline ajingi

  • Sr. Member
  • ****
  • Join Date: Feb 2004
  • Location: North
  • Posts: 272
    • View Profile
Re: Guidance for writing C++ code
« Reply #2 on: February 23, 2004, 11:58:14 AM »
Hello Zezezee!!
Let's look at a real program:

#include <iostream.h>
int main()
{
  cout<<"HEY, you, I'm alive!  Oh, and Hello World!";
  return 0;    
}

Let's look at the elements of the program one after the other. The #include is a preprocessor directive that tells the compiler to put code from the header file iostream.h into our program. By including header files, you an gain access to many different functions. For example, the cout function requires iostream.h.


The next imporant line is int main(). This line tells the compiler that there is a function named main, and that the function returns an integer, hence int. The braces ({ and }) signal the beginning and end of functions and other code blocks. If you have programmed in Pascal, you will know them as BEGIN and END.

The next line of the program may seem strange. If you have programmed in another language, you might expect that print would be the function used to display text. In C++, however, the cout function is used to display text. It uses the <;<; symbols, known as insertion operators. The quotes tell the compiler that you want to output the literal string as-is. The semicolon is added onto the end of all function calls in C++; the semicolon later shows up when you declare variables.

The penultimate line of code orders main to return 0. When one returns a value from main, it is passed on to the operating system. As a note, declaring main as though it had no return type - void main() - usually works; it is considered bad practice, however.

Class Declarations
Classes are declared in the following order: public member functions, protected member functions, private member functions, protected data members, and private data members. No data members may be declared public. For example:
class Application {
public:
 ? ?Application();
 ? ?~Application();
 ? ?void Run();
protected:
 ? ?void EventLoop();
private:
 ? ?EventRecord itsLastMouseEvent;
}
Control Structures
The control structures are as follows:
if (expression) {
 ? ?statements;
} else {
 ? ?statements;
}

for (expression; expression; expression) {
 ? ?statements;
}

do {
 ? ?statements;
} while (expression);

while (expression) {
 ? ?statements;
}

switch (expression) {
case constant:
 ? ?statements;
 ? ?break;
default:
 ? ?statements;
 ? ?break;
}

/* ?
 * long comments which need more than one line use
 * this block comment style, if you're writing C
 */

//
// if you're writing C++, your block comments should look more
// like this -- it's ugly to mix commenting styles
//

statement; ? ? ? ? ? /* short C comments */
statement; ? ? ? ? ? // short C++ comments

Thnx for your comment zezezee
I object to violence because when it appears to do good, the good is only temporary; the evil it does is permanent.

Offline zezezee

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Location: Kano
  • Posts: 372
    • View Profile
Re: Guidance for writing C++ code
« Reply #3 on: February 24, 2004, 11:46:52 PM »
u r soo koooooll man!!
i have never actually worked with c++ b4 but i think i will start veru soon. I have only worked with VB but this is cool. Thanx and keep ur posted ....i little dabaru here and there is wat makes a program good i think....


so spread da word ma men!!!...........
 just realised that nothing is what it seems.

Offline Dante

  • Sr. Member
  • ****
  • Join Date: Jan 2004
  • Location: Arewa ta kudu
  • Posts: 363
  • Gender: Male
    • View Profile
    • http://www.muntata.2ya.com/
Re: Guidance for writing C++ code
« Reply #4 on: February 26, 2004, 12:26:26 AM »
SO nice to have C lang. guru'z here!

Keep us upto date bro!
_________________________
Gaskiya tafi komai..........هو الذي

Offline ajingi

  • Sr. Member
  • ****
  • Join Date: Feb 2004
  • Location: North
  • Posts: 272
    • View Profile
Re: Guidance for writing C++ code
« Reply #5 on: March 28, 2004, 03:04:32 PM »
Insha Allah
I object to violence because when it appears to do good, the good is only temporary; the evil it does is permanent.

Offline zezezee

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Location: Kano
  • Posts: 372
    • View Profile
Guidance for writing C++ code
« Reply #6 on: October 30, 2004, 04:12:09 PM »
i dnt knw where u been man? i think we can do some c programming init ma men! mr ajingi,

a am tryin to pass a matrix to a function which in turn displays the matrix. 2 arguments r to be passed , the matrix itself, and an interger, n, which is just a counter.

#include <stdio.h>
#include <math.h>
#define SIZE 10

void displaymatrix (int [][SIZE], int);

int main ()
{
   int i, j, n, pass, hold;
                int matrix[SIZE][SIZE], sum1[SIZE];
   
   printf ("Please enter dimension of matrix, n(1 - 10)\n");
   scanf("%d",&n);  //get the size of the matrix
   
   for (i=0; i<=n-1; i++){
         for (j=0; j<=n-1; j++){
      printf("a[%d][%d]=",i,j);
      scanf("%d",&matrix[j]);
      printf("\n");
       }
   }
   
   displaymatrix (matrix[j],n); //displays the matrix to the user

   //calculate the sums of the columns
   for (j=0; j<=n-1; j++){
   sum1[j]=0;
                     for (i=0; i<=n-1; i++){
        printf("element[%d][%d] = %d \n",i,j,matrix[j]);
        sum1[j] = sum1[j] + fabs(matrix[j]);
       }
   printf("sum [%d]= %d\n",j,sum1[j]);
   getchar();
                }

   //bubbleup //sort out the matrix
      for (pass=1; pass<=n-1; pass++){
                     for (i=0; i<=n-2; i++){
                           if (sum1>sum1[i+1]){
                           hold=sum1;
                           sum1=sum1[i+1];
                           sum1[i+1]=hold;
                           }
                     }
                }
   
    print the l1 norm to the user
    printf("The L1 Norm is = %d", sum1[i=n-1]);
    getchar();
}

void displaymatrix(int matrix[][SIZE], int n)
{
//prints the matrix as it is to the user
    int i,j;
   for (i=0; i<=n-1; i++){
        for (j=0; j<=n-1; j++){
                     printf("%4d", matrix[j]);
        }
   printf("\n");
   }
}

Now, the issue here is the fuction is not takin the augument rightly!!! pls help is needed here...thanks guys :)  :)
 just realised that nothing is what it seems.

Offline zezezee

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Location: Kano
  • Posts: 372
    • View Profile
Guidance for writing C++ code
« Reply #7 on: November 02, 2004, 02:55:54 PM »
its all sorted out now.....so dnt bother anymore.....

thnks alot anyways
 just realised that nothing is what it seems.

Offline _Waziri_

  • Sr. Member
  • ****
  • Join Date: May 2004
  • Posts: 480
    • View Profile
Guidance for writing C++ code
« Reply #8 on: November 10, 2004, 04:03:32 PM »
Hey zezezee will dat ur program work?
I thot functions in programing return values only unlike procedures which perform tasks like dat of computations and printing results? Anuway dunno much of C++ pls communicate.

Anonymous

  • Guest
Guidance for writing C++ code
« Reply #9 on: November 10, 2004, 04:12:29 PM »
good question man....

as u can see, one or more of the functions actually return a 'void' meaning they dont actually return a value. they perform a task. if it were in VB, a subroutine wldve been used instead, but c doesnt have subroutines, so u only work with functions.

Offline zezezee

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Location: Kano
  • Posts: 372
    • View Profile
Guidance for writing C++ code
« Reply #10 on: November 10, 2004, 04:16:30 PM »
so i was not logged in...anyway, tha was me wazz....i wonder  :?:  r u waziri or _waziri_?
 just realised that nothing is what it seems.

Offline _Waziri_

  • Sr. Member
  • ****
  • Join Date: May 2004
  • Posts: 480
    • View Profile
Guidance for writing C++ code
« Reply #11 on: November 11, 2004, 03:44:07 PM »
Lol, zezezee, I am both.

But just a minute don't to u think some aspect of your program are just redundant. Like d sorting what does it do?

Offline zezezee

  • Sr. Member
  • ****
  • Join Date: Oct 2003
  • Location: Kano
  • Posts: 372
    • View Profile
Guidance for writing C++ code
« Reply #12 on: November 11, 2004, 07:02:04 PM »
naaah, no redundancy there...i think. wat that function does is that it checks the array passed and sorts it in ascending order........then the last element in the array becomes the maximum....number.

look at the code carefully n see
 just realised that nothing is what it seems.

 


Powered by EzPortal