Author Topic: C++ Lessons  (Read 5248 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3745
    • Oxygen Basic
C++ Lessons
« on: August 11, 2011, 10:33:03 AM »

This time using g++ on Ubuntu

g++ u1.cpp

./a.out


u1
Code: C++
  1. // classes example
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class CRectangle {
  6.     int x, y;
  7.   public:
  8.     void set_values (int,int);
  9.     int area () {return (x*y);}
  10. };
  11.  
  12. void CRectangle::set_values (int a, int b) {
  13.   x = a;
  14.   y = b;
  15. }
  16.  
  17. int main () {
  18.   CRectangle rect;
  19.   rect.set_values (3,4);
  20.   cout << "area: " << rect.area();
  21.   return 0;
  22. }
  23.  


u2
Code: C++
  1. // example: class constructor
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class CRectangle {
  6.     int width, height;
  7.   public:
  8.     CRectangle (int,int);
  9.     int area () {return (width*height);}
  10. };
  11.  
  12. CRectangle::CRectangle (int a, int b) {
  13.   width = a;
  14.   height = b;
  15. }
  16.  
  17. int main () {
  18.   CRectangle rect (3,4);
  19.   CRectangle rectb (5,6);
  20.   cout << "rect area: " << rect.area() << endl;
  21.   cout << "rectb area: " << rectb.area() << endl;
  22.   return 0;
  23. }

u3
Code: C++
  1. // example on constructors and destructors
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class CRectangle {
  6.     int *width, *height;
  7.   public:
  8.     CRectangle (int,int);
  9.     ~CRectangle ();
  10.     int area () {return (*width * *height);}
  11. };
  12.  
  13. CRectangle::CRectangle (int a, int b) {
  14.   width = new int;
  15.   height = new int;
  16.   *width = a;
  17.   *height = b;
  18. }
  19.  
  20. CRectangle::~CRectangle () {
  21.   delete width;
  22.   delete height;
  23. }
  24.  
  25. int main () {
  26.   CRectangle rect (3,4), rectb (5,6);
  27.   cout << "rect area: " << rect.area() << endl;
  28.   cout << "rectb area: " << rectb.area() << endl;
  29.   return 0;
  30. }
  31.  

u4
Code: C++
  1. // overloading class constructors
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class CRectangle {
  6.     int width, height;
  7.   public:
  8.     CRectangle ();
  9.     CRectangle (int,int);
  10.     int area (void) {return (width*height);}
  11. };
  12.  
  13. CRectangle::CRectangle () {
  14.   width = 5;
  15.   height = 5;
  16. }
  17.  
  18. CRectangle::CRectangle (int a, int b) {
  19.   width = a;
  20.   height = b;
  21. }
  22.  
  23. int main () {
  24.   CRectangle rect (3,4);
  25.   CRectangle rectb;
  26.   cout << "rect area: " << rect.area() << endl;
  27.   cout << "rectb area: " << rectb.area() << endl;
  28.   return 0;
  29. }

u5
Code: C++
  1. // pointer to classes example
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class CRectangle {
  6.     int width, height;
  7.   public:
  8.     void set_values (int, int);
  9.     int area (void) {return (width * height);}
  10. };
  11.  
  12. void CRectangle::set_values (int a, int b) {
  13.   width = a;
  14.   height = b;
  15. }
  16.  
  17. int main () {
  18.   CRectangle a, *b, *c;
  19.   CRectangle * d = new CRectangle[2];
  20.   b= new CRectangle;
  21.   c= &a;
  22.   a.set_values (1,2);
  23.   b->set_values (3,4);
  24.   d->set_values (5,6);
  25.   d[1].set_values (7,8);
  26.   cout << "a area: " << a.area() << endl;
  27.   cout << "*b area: " << b->area() << endl;
  28.   cout << "*c area: " << c->area() << endl;
  29.   cout << "d[0] area: " << d[0].area() << endl;
  30.   cout << "d[1] area: " << d[1].area() << endl;
  31.   delete[] d;
  32.   delete b;
  33.   return 0;
  34. }
  35.  

Charles

efgee

  • Full Member
  • ***
  • Posts: 172
Re: C++ Lessons
« Reply #1 on: August 11, 2011, 05:48:02 PM »

Peter

  • Full Member
  • ***
  • Posts: 161
Re: C++ Lessons
« Reply #2 on: August 12, 2011, 01:37:40 AM »
fortunately, I have no Linux/Ubuntu.
 

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3745
    • Oxygen Basic
Re: C++ Lessons
« Reply #3 on: August 12, 2011, 04:00:31 AM »

I know about D but I have not been tempted to investigate yet. Is there any thing useful we could add to Oxygen?

Ubuntu is a far away country, and slightly outside my comfort zone. I also have Mandriva pre-installed on a Linux PC. I am more familiar with this environment.

The main problem I have is knowing what to do when a package installation does not work automatically. For instance, installing the Flash plugin for Firefox on Ubuntu.

Charles

Aurel

  • Guest
Re: C++ Lessons
« Reply #4 on: August 12, 2011, 04:11:12 AM »
Linux as platform - haaa
What to say when 50% of distros don't recognize ELF as native linux executable
format ::) ::) ::)

from:
What Linux Will Look Like In 2012
Linux will continue to mature and evolve into an operating system that non-technical users can fully embrace.
Such a lie...
Many linux users ( from what i see on our Crotian linux forum) says that there is no bright future for linux in any way -maby only as server  OS)
« Last Edit: August 12, 2011, 04:17:21 AM by Aurel »

Peter

  • Full Member
  • ***
  • Posts: 161
Re: C++ Lessons
« Reply #5 on: August 12, 2011, 05:09:32 AM »
this is what i thought, it has no future at all. there is never a future for this nonsense.
the future comes from Microsoft, greet Windows with cheers.   :D

John

  • Hero Member
  • *****
  • Posts: 3249
Re: C++ Lessons
« Reply #6 on: August 12, 2011, 09:00:10 AM »
Quote
the future comes from Microsoft, greet Windows with cheers.

This is a joke, right?

Peter

  • Full Member
  • ***
  • Posts: 161
Re: C++ Lessons
« Reply #7 on: August 12, 2011, 09:24:28 AM »
your sudden appearance horrifies me.

Okay, I got   -2 .  :D   

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3745
    • Oxygen Basic
Re: C++ Lessons
« Reply #8 on: August 12, 2011, 12:15:57 PM »
We may end up with a single operating system in the same way that x386 processors have displaced Sparc, Motorola, & PowerPC to name a few.

There are also new OS contenders from strong players like Google and Hewlett-Packard. They are in a strong position because their primary income is not dependent of selling software and therefore there is no motive either to play Monopoly or to fragment the market.

Charles

Peter

  • Full Member
  • ***
  • Posts: 161
Re: C++ Lessons
« Reply #9 on: August 14, 2011, 04:44:54 AM »
lesson c++  Prefix/Postfix

Code: [Select]
#include <iostream.h>

int a=10;
int b=10;
int c=0;
int d=0;

void main(void)
{
  c = b++;
  cout << "b:" << b << " c:" << c << '\n';
  
  d = --a;
  cout << "a:" << a << " d:" << d << '\n';
}


lesson OxygenBasic  Prefix/Postfix

Code: [Select]
include "window.h"
def color 0xFFC8C8

setwindow "Prefix/Postfix",640,480,ws_overlapped
setfont 16,22,0,0

sys a=10,b=10,c,d

c = b: b = b+1
settext "b:" + b + " c:" + c,32,32,color

a = a-1
d = a
settext "a:" + a + " d:" + d,32,64,color

waitkey(vk_space)
winend


[attachment deleted by admin]
« Last Edit: August 14, 2011, 04:50:12 AM by peter »

Peter

  • Full Member
  • ***
  • Posts: 161
Re: C++ Lessons
« Reply #10 on: August 14, 2011, 05:33:45 AM »
lesson c++  function overload

Code: [Select]
#include <iostream.h>

void output(int x);
void output(char c);
void output(float f,char c);

void main(void)
{
  int   intvar    = 10;
  char  Character = 'Z';
  float fvar      = 11.11f;
 
  output(character);
  output(intvar);
  output(fvar,Character);
}

void output(int x)
{
  cout << x << '\n';
}

void output(char c)
{
  cout << c << '\n';
}

void output(float f, char c)
{
  cout << f << " " << c << '\n';
}

efgee

  • Full Member
  • ***
  • Posts: 172
Re: C++ Lessons
« Reply #11 on: August 14, 2011, 12:03:59 PM »
I know about D but I have not been tempted to investigate yet. Is there any thing useful we could add to Oxygen?

Well having coded in C/C++/D I must admit D feels like a BASIC language with C syntax.

BASIC's have reference counting as a mechanism to dispose/free memory. D has an automatic garbage collector that takes care of this.
Namespaces are different: in D each module is a namespace; so if a module is imported with "import" everything is taken care of. (Maybe Oxygen can do this as well.)
Arrays are one of the best I've ever seen and the syntax feels closer to C than C++ to C.

Have yet to find something were I think: "This is awkward..."; in C++ this happens all the time.


John

  • Hero Member
  • *****
  • Posts: 3249
Re: C++ Lessons
« Reply #12 on: August 14, 2011, 02:35:29 PM »
I installed D for Ubuntu 64 and compiled (optimized) their Hello World! example.

892,764 bytes

Wow! D's runtime seems a bit bloated.

Code: [Select]
#!/usr/bin/dmd -run
/* sh style script syntax is supported */

/* Hello World in D
   To compile:
     dmd hello.d
   or to optimize:
     dmd -O -inline -release hello.d
*/

import std.stdio;

void main(string[] args)
{
    writeln("Hello World, Reloaded");

    // auto type inference and built-in foreach
    foreach (argc, argv; args)
    {
        // Object Oriented Programming
        auto cl = new CmdLin(argc, argv);
        // Improved typesafe printf
        writeln(cl.argnum, cl.suffix, " arg: ", cl.argv);
        // Automatic or explicit memory management
        delete cl;
    }

    // Nested structs and classes
    struct specs
    {
        // all members automatically initialized
        size_t count, allocated;
    }

    // Nested functions can refer to outer
    // variables like args
    specs argspecs()
    {
        specs* s = new specs;
        // no need for '->'
        s.count = args.length;   // get length of array with .length
        s.allocated = typeof(args).sizeof; // built-in native type properties
        foreach (argv; args)
            s.allocated += argv.length * typeof(argv[0]).sizeof;
        return *s;
    }

    // built-in string and common string operations
    writefln("argc = %d, " ~ "allocated = %d",
argspecs().count, argspecs().allocated);
}

class CmdLin
{
    private size_t _argc;
    private string _argv;

public:
    this(size_t argc, string argv) // constructor
    {
        _argc = argc;
        _argv = argv;
    }

    size_t argnum()
    {
        return _argc + 1;
    }

    string argv()
    {
        return _argv;
    }

    string suffix()
    {
        string suffix = "th";
        switch (_argc)
        {
          case 0:
            suffix = "st";
            break;
          case 1:
            suffix = "nd";
            break;
          case 2:
            suffix = "rd";
            break;
          default:
   break;
        }
        return suffix;
    }
}

jrs@laptop:~/D$ dmd -O -inline -release test.d
jrs@laptop:~/D$ ls -l
total 960
-rwxr-xr-x 1 jrs jrs 892764 2011-08-14 15:29 test
-rwxr-xr-x 1 jrs jrs   1959 2011-08-14 15:24 test.d
-rw-r--r-- 1 jrs jrs  82628 2011-08-14 15:29 test.o
jrs@laptop:~/D$ ./test
Hello World, Reloaded
1st arg: ./test
argc = 1, allocated = 22
jrs@laptop:~/D$
« Last Edit: August 14, 2011, 03:13:35 PM by JRS »

John

  • Hero Member
  • *****
  • Posts: 3249
Re: C++ Lessons
« Reply #13 on: May 23, 2018, 05:57:07 PM »
Quote from: Fred Harris
There isn't much difference between building 32 bit or 64 bit executables.  The language in large degree insulates you from the differences.

The best way I think for someone wishing to learn C is to use Script BASIC and create extension modules with C BASIC.