News:

Ramadan Mubarak!

I pray that we get the full blessings of Ramadan and may Allah (SWT) grant us more blessings in the year to come.
Amin Summa Amin.

Ramadan Kareem,

Main Menu

Guidance for writing C++ code

Started by ajingi, February 21, 2004, 03:53:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ajingi

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.

zezezee

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.

ajingi

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.

zezezee

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.

Dante

SO nice to have C lang. guru'z here!

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

ajingi

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

zezezee

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.

zezezee

its all sorted out now.....so dnt bother anymore.....

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

_Waziri_

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

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.

zezezee

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.

_Waziri_

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?

zezezee

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.