USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Learning C++ on a Linux Computer

 
Post new topic   This topic is locked: you cannot edit posts or make replies.   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 3:44 pm    Post subject: Learning C++ on a Linux Computer Reply with quote

Since I have been running Linux for a couple years on and off, and now all the time, I have decided to jump in even deeper and learn C++, all on my Linux Mandrake computer.

I will be using the gcc compiler. This is available free with almost any Linux distrobution. It even works running Knoppix, a free cd based linux distro that you don't even have to install to run.....but that's another story.

I'm sure I will make mistakes, and hopefully get them corrected. Please feel free to post your comments if you see a mistake. I'm human............and I make ALOT of mistakes........ lol.

I learn best by DOING, ......that's why this forum is here. Several things that people take for granted a newbie (like myself) struggles with. Perhaps my struggles will ease others learning

I have decided that I don't really like the Windows OS monopoly, and want to help make Linux a better OS and help improve applications where I can. Since alot of programs and applications use C and C++ it seemed prudent to start learning this. After learning C++ other languages should be a little easier to master..........but why mess with the small fish eh???

So............here I go..........diving right in........ please be patient Very Happy




Last edited by crouse on Fri Sep 22, 2024 5:11 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 3:46 pm    Post subject: Creating your first program - Where to start. Reply with quote

I have decided to use Kate, a multifunctional text editor for learning c++ for now. My reasons, it's easy to use....straightforward and has nice syntax highlighting. I know I know....it ain't vi or emacs....but I haven't taken the time to learn those yet...and prefer to concentrate on C++ for now.This may change of course at a later date...so don't beat me up to bad about my text editor choice

When creating a c++ program, the stuff you type into the text editor is called the Source File. Similar to any other text file..........but instead of a .txt extension you might normally associate with a text file, you use .cpp or .cp or .c or .cxx The extension I'm going to use is .cpp because it just seems to make the most sense to me. So when creating my program the first thing I must do is type in the code into a text editor, then save it as a .cpp file. We will say I saved this one as Program1.cpp From there, open up a terminal window and we need to compile it in order to have a finished c++ program.

To compile a c++ program.......... in your terminal window cd to the directory where your source file is located. Then you type the following to make Program1.cpp source file a c++ program.

g++ Program1.cpp -o Program1

I should now have an executable file named Program1 in the same directory as my source file.Here's what it does....sort of....

g++ tells the computer to fire up the compiler
Program1.cpp is the source file we want to make into a program.
-omeans to "output" to a file named (whatever you type next)
Program1 would be the final program's name........this could be anything you want to name it.......it doesn't have to follow the .cpp files name. It could have very easily been named SCOOBY ..... Keeping the source file name and the Program name the same........just seems to make sense for me. Easier to find the source file and edit it........... if it has the same name.


Pretty darn simple eh? How do you run that program you ask..........well that's pretty darn simple too! At the command line of your terminal window type the following command:

./Program1 <<<<<note the period at the beginning of the command !!!

Hit the enter key ........ and it executes in the terminal. You must be in the Directory where Program1 resides of course. There are no spaces between the "dot slash name" command. That is a down and dirty quick overview of how your first program will work.

The next post will deal with the code needed to create the first Source File !!!


Back to top
View user's profile Send private message Visit poster's website
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 3:50 pm    Post subject: Program 1 -- Hello World Reply with quote

Now we are going to create our first program. Following in tradition of countless eons, it's of course the Hello World program. (I sure hope I make it past this... lol)

Here goes:
-------
#include <iostream.h>
int main();
int main()
{
cout << "Hello world, this is my first c++ program !!! \n";
return 0;
}

-------
This works for Mandrake 9.0 using the gcc compiler that was installed with it. If you are using another version, or compiler........you should read up on the compiler to know what commands are needed.

You can copy this into your text editor ......... save it as Program1.cpp and then compile it using the command
g++ Program1.cpp -o Program1

Run your compiled program using the command:
./Program1

Our first program in C++ is now done!!


Back to top
View user's profile Send private message Visit poster's website
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 3:57 pm    Post subject: Program 2 - (float) endl and addition Reply with quote

Nothing real earth shattering...but some new stuff.


#include <iostream.h>
int main();
int main()
{
/* This is the way to create a large block of comments
Everything bewteen the slash-star and then the star-slash
is commented out */

// The double slash comments out one line only. Comments don't affect your program at all.

// How to print one line of text
cout << "Hello world, this is my second c++ program !!! \n";

// How to add two numbers together
cout << "This is shows you how you could add two numbers 12 and 13 : " << 12+13 << endl;

// How to convert a fraction to a decimal using the term (float)
cout << "This is a fraction written as a decimal 5/8=" << (float) 5/8 << endl;

// How to create a blank line
cout << " " << endl;
cout << "The above will create a blank line." << endl;

// How to create a blank line
cout << "\n";
cout << "The above will create a blank line." << endl;

// How to perform an operation and continue with the text.
cout << "This is a fraction written as a decimal 5/8=" << (float) 5/8 << " and this is text added after the operation is completed" << endl;
return 0;
}
[/b]


Back to top
View user's profile Send private message Visit poster's website
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 3:59 pm    Post subject: Program 3 - A simple function Reply with quote

#include <iostream.h>

/* This program shows how a simple function works. We will call the function Addition. In this program
will use the cin command to input 3 numbers and then add them together with the function Addition. Notice the
Addition function comes before the main function. */

int Addition (int x, int y, int z)
{
cout << "The Addition function recieved the numbers: " << x << " and " << y << " and " << z << "\n";
return (x+y+z);
}


int main()


{
cout << "This is the main() function \n";
int a, b, c, d;
cout << "Enter in 3 numbers to add up: ";
cin >> a;
cin >> b;
cin >> c;
cout << "Now calling Addition() function: \n";
d=Addition(a,b,c);
cout << "Back in the main() function now that the Addition () function has done it's work \n";
cout << "The three numbers you entered added up to: " << d << "\n";
cout << "\n";
cout << "Done \n";

return 0;
}


This program demostrates a simple function. It calls the function Addition from the main function. (Personally I call it a subroutine.... Very Happy )
[/b]


Back to top
View user's profile Send private message Visit poster's website
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 4:06 pm    Post subject: Program 4 --- Area of a Triangle Reply with quote

#include <iostream.h>

/* This is to Figure the Area of a Triangle */

float areaoftriangle (float x, float y)
{

return (x*y/2);
}


int main()


{
cout << "\n";
cout << "\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";
cout << " AREA OF A TRIANGLE \n";
cout << "\n";
cout << "\n";
cout << "To figure the area of a triangle we need to \n";
cout << "have the base width and overall height of the triangle. \n";
cout << "The formula for the area of a triangle is :\n";
cout << " BASE X WIDTH / 2 = AREA \n";
float a, b, c ;
cout << "\n";
cout << "Please enter the BASE WIDTH of the triangle: ";
cin >> a;
cout << "Now enter the OVERALL HEIGHT of the triangle: ";
cin >> b;
cout << "\n";
cout << "\n";
c=areaoftriangle(a,b);
cout << "The AREA of the triangle with a base width of " << a << " and a height of " << b << " is " << c << " \n";

cout << "\n";
cout << "\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";
return 0;
}


Back to top
View user's profile Send private message Visit poster's website
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 4:08 pm    Post subject: C++ Variable Types Reply with quote

Here is a list of the most common c++ variable types.

Type....................Value
bool....................True or False
unsigned short int......0 to 65,535
short int...............-32,768 to 32,767
unsigned long int.......0 to 4,294,967,295
long int................-2,147,483,648 to 2,147,483,647
int (16-bit)............-32,768 to 32,767
int (32-bit)............-2,147,483,648 to 2,147,483,647
unsigned int (16-bit).....0 to 65,535
unsigned int (32-bit).....0 to 4,294,967,295
char......................256 char values
float.....................-1e-38 to 3.4e38
double....................-2.2e-308 to 1.8e308

Each variable type can take a different amount of memory. In this day and age of memory getting into the 1+ gig range, I'm not real sure that memory is a huge consideration in a small program....perhaps even a large one? All variable's are case sensitive. C is not the same as c.

It is possible to create/define a variable like so:

unsigned int myageis;

or to define more than one at the same time

unsigned int myageis, yourageis;

You can assign variables a value EXAMPLE:

unsigned int myageis;
myageis = 89;


You can eliminate some typing of long variable assigments like:

unsigned short int myageis;
by using the TYPEDEF keyword (type definition). This in effect lets you create an alias for the command. Now you don't have to keep retyping the long variable assignment..... you could instead:

typedef unsigned short int NSHORT; //typedef NSHORT is defined here this usually goes at the top of the program underneath the includes.

Now you can use NSHORT in place of unsigned short int EXAMPLE:
NSHORT myageis; is the same as
unsigned short int myageis;


Back to top
View user's profile Send private message Visit poster's website
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 4:09 pm    Post subject: Program #5-- Area of a Circle Reply with quote

#include <iostream.h>

/* This is to Figure the Area of a Circle */

float areaofcircle (float x, float y)
{

return ((x*x)*y);
}


int main()


{
cout << "\n";
cout << "\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";
cout << " AREA OF A CIRCLE \n";
cout << "\n";
cout << "\n";
cout << "To figure the area of a circle we need to \n";
cout << "have the radius and pi. \n";
cout << "The formula for the area of a circle is :\n";
cout << " PI X Radius(2) = AREA \n";
float a, b, c ;
cout << "\n";
cout << "Please enter the Radius of the circle: ";
cin >> a;
cout << "PI=3.1415 (for our purposes here): ";
b=3.1415 ;
cout << "\n";
cout << "\n";
c=areaofcircle(a,b);
cout << "The AREA of the circle with a radius of " << a << " is " << c << " \n";

cout << "\n";
cout << "\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";
return 0;
}


Back to top
View user's profile Send private message Visit poster's website
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 8985
Location: Iowa

PostPosted: Tue Apr 22, 2024 4:17 pm    Post subject: Program #6 Area of Circle with Loop Reply with quote

#include <iostream>
using namespace std;


/* This is to Figure the Area of a Circle,
Added a loop to this to allow for continued use
until the program encounters another number besides 1 */


float areaofcircle (float x, float y)
{

return ((x*x)*y);
}

int main();
int main()
{

int looping;


do
{
cout << "\n";
cout << "\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";
cout << " AREA OF A CIRCLE \n";
cout << "\n";
cout << "\n";
cout << "To figure the area of a circle we need to \n";
cout << "have the radius and pi. \n";
cout << "The formula for the area of a circle is :\n";
cout << " PI X Radius(2) = AREA \n";
float a, b, c ;
cout << "\n";
cout << "Please enter the Radius of the circle: ";
cin >> a;
cout << "PI=3.1415 (for our purposes here): ";
b=3.1415 ;
cout << "\n";
cout << "\n";
c=areaofcircle(a,b);
cout << "The AREA of the circle with a radius of " << a << " is " << c << " \n";
cout << "\n";
cout << "\n";
cout << "Would you like to do this again?? \n";
cout << "Type 1 to continue, or 0 to exit| \n";
cin >> looping;

}while ( looping == 1 );

cout << "\n";
cout << "Thanks for using my Area of a Circle program !! \n";
cout << "Good Bye !!\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";


return 0;
}



The above program loops until a number other than 1 is entered.

This also show relational operators in action. Specifically the EQUAL TO operator ==

Name..................Operator
.
Equals ==
Not Equals !=
Greater Than >
Greater Than or Equals >=
Less Than <
Less Than or Equals <=

------------------------------------------------
I am currently using Linux Mandrake 9.0

In order to appease the compiler on my system... below the #include ............ I need the following line
.
using namespace std;
.
in order to eliminate the errors I get when compiling. Those errors are:
program6.cpp:20: `cout' undeclared (first use this function)
program6.cpp:20: (Each undeclared identifier is reported only once for each function it appears in.)
program6.cpp:36: `cin' undeclared (first use this function)


Also the #include works better with no errors if I use:
#include <iostream> instead of
#include <iostream.h>

Just some thoughts in case anyone else wanted to know. Very Happy
-------------------------------------------------
This updated program allows any key to be used to exit the program. Now if a letter key is hit it exits gracefully :D



#include <iostream>
using namespace std;

/* This is to Figure the Area of a Circle,
Added a loop to this to allow for continued use
until the program encounters another number besides 1 */

float areaofcircle (float x, float y)
{return ((x*x)*y);}

int main()
{

int looping;

do{
cout << "\n";
cout << "\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";
cout << " AREA OF A CIRCLE \n";
cout << "\n";
cout << "\n";
cout << "To figure the area of a circle we need to \n";
cout << "have the radius and pi. \n";
cout << "The formula for the area of a circle is :\n";
cout << " PI X Radius(2) = AREA \n";
float a, b, c ;
cout << "\n";
cout << "Please enter the Radius of the circle: ";
cin >> a;
cout << "PI=3.1415 (for our purposes here): ";
b=3.1415 ;
cout << "\n";
cout << "\n";
c=areaofcircle(a,b);
cout << "The AREA of the circle with a radius of " << a << " is " << c << " \n";
cout << "\n";
cout << "\n";
cout << "Would you like to do this again?? \n";
cout << "Type 1 to continue, or any other key to exit.. \n";
cin >> looping;

if ( looping != 1 )
looping = 2;

}while ( looping == 1 );

cout << "\n";
cout << "Thanks for using my Area of a Circle program !! \n";
cout << "Good Bye !!\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";

return 0;
}


Back to top
View user's profile Send private message Visit poster's website
mr_ed
Moderator


Joined: 28 Aug 2024
Posts: 3306
Location: 42 miles north of Ogdensburg, NY

PostPosted: Fri Sep 22, 2024 6:29 pm    Post subject: Re: C++ Variable Types Reply with quote

crouse wrote:
Here is a list of the most common c++ variable types.

Type....................Value
bool....................True or False
unsigned short int......0 to 65,535
short int...............-32,768 to 32,767
unsigned long int.......0 to 4,294,967,295
long int................-2,147,483,648 to 2,147,483,647
int (16-bit)............-32,768 to 32,767
int (32-bit)............-2,147,483,648 to 2,147,483,647
unsigned int (16-bit).....0 to 65,535
unsigned int (32-bit).....0 to 4,294,967,295
char......................256 char values
float.....................-1e-38 to 3.4e38
double....................-2.2e-308 to 1.8e308


This is mostly correct... until you count in different architectures.

For example, an int (there is no way to specify 16 or 32 bit, btw... a 16-bit int is a short) is 32 bits on a 32-bit processor and 64 bits on a 64-bit processor... I think. Or maybe int = 32 and long = 64? The ANSI C++ standard only specifies that long >= int.

The only way to be guaranteed is by using sizeof(int) or std::numeric_limit<int>::max()

On all the platforms I've coded in C/C++ on (Intel/AMD x86, Motorola 68328, Intel PXA255, Intel IXP425), all have been 32-bit platforms, and all have had int == long == 32 bits, but even then, I never assume that it will continue to be so.



_________________
Desktop: Ubuntu 6.06 "Dapper Drake"
Server: Ubuntu 5.10 "Breezy Badger"
Laptop: Ubuntu 6.10 "Edgy Eft"
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2024 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2006 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse