July/August Challenge - C++ self teach

Are you up to the challenge?

Moderators: Tituba, BarbaraSher

Forum rules
Please start the monthly challenge threads here. All other off-topic threads will be removed.

July/August Challenge - C++ self teach

Postby Scenario Thinker » Mon Jul 06, 2009 7:29 am

<<<<<<<<<< I hope this is OK for this forum. If it belongs somewhere else, let me know. >>>>>>>>> I've gotten the semi-annual going-to-school bug, but a little early for fall semester. It started out I was taking some of my old math books on the train and starting to read them. First, linear algebra, then I switched to differential equations. I was toying with the idea of taking a math class in the fall to refresh. But, then that led to me thinking about taking programming and web classes. I've always wanted to learn C++ well enough to say I've learned it. I've started umpteen times reading books on it, but never really get that far. I did have a C class a long time ago, but it was one credit and the main language they used to teach computer science was Pascal for some reason, so even though I learned the CS concepts with Pascal, after taking the one credit C class (versus 3 for a normal semester class), I didn't learn it well enough and then burned out on continuing the CS classes converting over to C (which they did from classes there on). I think they should have started using C from the beginning, and now most CS schools do. Then there's web design. I have had a domain name for a couple years now, and I always wanted to create my own web site. So far, just a micro bare bones HTML page is out there now. There are also classes on creating web sites, then later scripting ,etc. My dilemma is always what do I take and how much. My brain is usually bigger than my stomach and I want to take everything, but realistically, I like only one class at a time working full time. So, I decide that this summer, I'd start exploring what's on the web mostly for free, and see if anything sticks by fall, then decide if I even need a class anymore, or maybe I can continue anyhow, but would know better which direction to go. I've done a couple things so far. I discovered that with Microsoft, you can download their Visual Studio Express C++ IDE (Integrated Development Environment) for free. Also, I downloaded an HTML editor called Coffee Cup on a 30 day trial, but it's only $49 if you buy it (versus something like Dreamweaver for a few hundred). I played with Coffee Cup a little, and I'll have to wait and see if anything sticks there. Well, the C++ IDE took a while to install, but once I went in there, they suggested a course by Herbert Schildt that is turning out to be excellent in learning C++. Part of the advantage is I'm writing and running the example programs, and it really helps to learn by doing. I'm using the command line to compile (which he suggests for the entire book). I only use the IDE for editing and saving the programs. SO ... after a few days, I have completed 4 chapters out of 12. Mostly, this was basic C programming which I know I've learned once. Now, it's starting to get into the heavy stuff which I haven't studied a lot like classes and object oriented programming. I thought I'd use this thread to track my progress and use it as a log to keep my motivated. I'll try and record significant things that I learn, stumbling blocks, etc. Anyone can chime in if you know anything about this stuff (C++ or web stuff).
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Mon Jul 06, 2009 7:29 am

Chapter 1
Chapter 1: C++ Fundamentals This module will introduce you to C++, its history, its design philosophy, and several of its most important features. This module provides a brief overview of several C++ features, including the general form of a C++ program, some basic control statements, and operators. It does not go into too many details, but rather concentrates on the general concepts common to any C++ program.
Completed July 3/4/5, 2009
Last edited by Scenario Thinker on Sat Jul 11, 2009 4:07 pm, edited 1 time in total.
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Mon Jul 06, 2009 7:29 am

Chapter 2
Chapter 2: Introducing Data Types and Operators At the core of a programming language are its data types and operators. As you might expect, C++ supports a rich assortment of both data types and operators, making it suitable for a wide range of programming. This module will cover an examination of C++’s foundational data types and its most commonly used operators. We will also take a closer look at variables and examine the expression.
Completed July 3/4/5, 2009
Last edited by Scenario Thinker on Sat Jul 11, 2009 4:09 pm, edited 1 time in total.
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Mon Jul 06, 2009 7:30 am

Chapter 3
Chapter 3: Program Control Statements This module discusses the statements that control a program’s flow of execution. There are three categories of program control statements: selection statements, which include the if and the switch; iteration statements, which include thefor, while, and do-while loops; and jump statements, which include break, continue,return, and goto.
Completed July 3/4/5, 2009
Last edited by Scenario Thinker on Sat Jul 11, 2009 4:10 pm, edited 2 times in total.
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Mon Jul 06, 2009 7:30 am

Chapter 4
Chapter 4: Arrays, Strings, and Pointers This module discusses arrays, strings, and pointers. An array is a collection of variables of the same type that are referred to by a common name. Arrays offer a convenient means of creating lists of related variables. The C++ language does not define a built-in string data type. Instead, strings are implemented as arrays of characters. A pointer is an object that contains a memory address. Typically, a pointer is used to access the value of another object.
Completed July 3/4/5, 2009
Last edited by Scenario Thinker on Sat Jul 11, 2009 4:11 pm, edited 1 time in total.
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Unity » Mon Jul 06, 2009 11:59 am

Good Luck ST :D
Unity
Mega Poster
Mega Poster
 
Posts: 7815
Joined: Fri Mar 19, 2004 11:01 pm

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Sat Jul 11, 2009 4:18 pm

Chapter 5
Chapter 5: Introducing Functions This module begins an in-depth discussion of the function. Functions are the building blocks of C++, and a firm understanding of them is fundamental to becoming a successful C++ programmer. Here, you will learn how to create a function. You will also learn about passing arguments, returning values, local and global variables, function prototypes, and recursion.
Completed July 11, 2009 I won't be getting into the really new stuff for me until chapter 8. But, it's good review and practice. The one credit C class I had before consisted of about chapters 1-7. Since it was only one credit versus the typical three, they didn't go into very much detail, but I think it would have followed this book pretty closely as far as depth, because this book is a beginning book, but I like it for a good foundation.
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Sun Jul 12, 2009 12:34 pm

Chapter 6
Chapter 6: A Closer Look at Functions This module continues our examination of the function. It discusses three of C++’s most important function-related topics: references, function overloading, and default arguments.
Completed July 12, 2009
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Sun Jul 19, 2009 6:26 am

Chapter 7
Chapter 7: More Data Types and Operators This module returns to the topics of data types and operators. In addition to the data types that you have been using so far, C++ supports several others. Some of these consist of modifiers added to the types you already know about. Other data types include enumerations and typedefs. C++ also provides several additional operators that greatly expand the range of programming tasks to which C++ can be applied.
Completed July 18,19, 2009 I guess this is what they mean by a bithead:
Code: Select all
#include <iostream>  #include <cstdlib>  #include <cstdio>  #include <cstring>  #include <cctype>  using namespace std;   unsigned char lrotate(unsigned char val, int n); unsigned char rrotate(unsigned char val, int n); void show_binary (unsigned int u);  int main()  {   char ch = 'T';  int i;   cout << "Original value in binary:
";  show_binary(ch);   cout << "Rotating right 8 times:
";  for (int i=0; i<8; i++){   ch = rrotate(ch, 1);   show_binary(ch);  }    cout << "Rotating left 8 times:
";  for (int i=0; i<8; i++){   ch = lrotate(ch, 1);   show_binary(ch);  }    return 0;   }  void show_binary (unsigned int u) {  int t;  /* 128 = 1 0 0 0 0 0 0 0      64 = 0 1 0 0 0 0 0 0   32 = 0 0 1 0 0 0 0 0   16 = 0 0 0 1 0 0 0 0    8 = 0 0 0 0 1 0 0 0    4 = 0 0 0 0 0 1 0 0    2 = 0 0 0 0 0 0 1 0    1 = 0 0 0 0 0 0 0 1     256 = 0 0 0 0 0 0 0 1  0 0 0 0 0 0 0 0     512 = 0 0 0 0 0 0 1 0  0 0 0 0 0 0 0 0    1024 = 0 0 0 0 0 1 0 0  0 0 0 0 0 0 0 0    2048 = 0 0 0 0 1 0 0 0  0 0 0 0 0 0 0 0    4096 = 0 0 0 1 0 0 0 0  0 0 0 0 0 0 0 0    8192 = 0 0 1 0 0 0 0 0  0 0 0 0 0 0 0 0   16384 = 0 1 0 0 0 0 0 0  0 0 0 0 0 0 0 0   32768 = 1 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0    */ // for (t=32768; t>0; t/=2)  for (t=128; t>0; t/=2)   if (u & t) cout << "1 ";   else cout << "0 ";   cout << '
'; }  unsigned char lrotate(unsigned char val, int n) {  unsigned int t;   t = val;   for (int i=0; i<n; i++){   t = t << 1;   /*   0000 0000 1111 1111   0000 0001 1111 1110 <--- shifted into lower bit in second byte.  */    /*    If a bit shifts out, it will be in bit 8 of the integer t.   If this is the case, put a 1 on the right side.   */   if (t & 256)    t = t | 1; // put a 1 on the right end  }  return t; // return the lower 8 bits }  unsigned char rrotate(unsigned char val, int n) {  unsigned int t;   t = val;   // First, move the value 8 bits higher  t = t << 8;  /*   0000 0000 1111 1111   1111 1111 0000 0000 <--- shifted to second byte  */   for (int i=0; i<n; i++){   t = t >> 1;  /*   0000 0000 1111 1111   0111 1111 1000 0000 ---> shifted to highest bit of first byte  */    /*    If a bit shifts out, it will be in bit 7 of the integer t.   If this is the case, put a 1 on the left side.   */   if (t & 128)    t = t | 32768; // put a 1 on the left end  }  // move the result back to the lower 8 bits of t.  t = t >> 8;   return t; // return the lower 8 bits }
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Sat Jul 25, 2009 3:29 pm

Chapter 8
Chapter 8: Classes and Objects The class is C++’s basic unit of encapsulation. Classes are used to create objects. To write object-oriented programs, you will need to use classes. Classes and objects are so fundamental to C++ that much of the remainder of this book is devoted to them in one way or another.
Completed July 25, 2009 Fun stuff. A class on classes :)
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Sat Aug 01, 2009 3:48 pm

Chapter 9
Chapter 9: A Closer Look at Classes This module continues the discussion of the class begun in Module 8. It examines a number of class-related topics, including overloading constructors, passing objects to functions, and returning objects. It also describes a special type of constructor, called the copy constructor, which is used when a copy of an object is needed. Next, friend functions are described, followed by structures and unions, and the this keyword. The module concludes with a discussion of operator overloading, one of C++’s most exciting features.
ALMOST Completed August 01, 2009 Operator overloads? Who thinks up this sh*t? :) Well, I guess Bjarne Stroustrup does, he created C++. It's one of the most exciting features according to above. The stuff is getting quite sluggy right now, more slow going than earlier chapters. But, I ordered several more C++ books and they've come, so I can reference them on these topics to get a different point of view. I can't quite get this last program to work in chapter 9, but I need a break to go watch some TV. I think there might be a missing piece, because I did just what he says in the book, and, I haven't really found an error in the book yet, amazing. What I mean is all the programs that are created in the book compile and run without changing anything from the book.
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Sun Aug 02, 2009 5:17 pm

Well, I never got to chapter 10, but I spent some time trying to find my bug in chapter 9. It turned out is was a not equal (!=) versus an equal (==). I had copied some similar code from further up in the program, and didn't notice that the logic switched. To find the bug, I ended up downloading the code that comes with the book and doing a line by line compare, even using the help of a text editor to do the comparing to finally SEE IT!!! Jeeez. The function find sets a flag equal to a positive number if a member is in a list (the number it is in the list), and -1 if it's not. The function isMember calls find and if it's NOT EQUAL to -1, then it sets itself to true, otherwise false. So, I copied that code to go inside the operator to add a new member to the list and there it calls find and if it's EQUAL to -1 (meaning it's not there), then add it. Well, what I did is say it's always there, so it never adding anything! By the way, if I say NOT isMember in the operator, that works too. Completed Chapter 9: A Closer Look at Classes August 02, 2009 Here's the program with all my messy debugging output, etc.:
Code: Select all
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cctype> using namespace std;  const int MaxSize = 100;  class Set {  int len; // number of members  char members[MaxSize]; // this array holds the set   // find() function private, because not used outside of the Set class  int find(char ch); // find an element (Book had this, download had next line)  //int Set::find(char ch); // find an element (Download had this, book had previous line)  public:  // Construct a null set  //Set() {len = 0; cout << "Constructed...
";}  Set() {len = 0;}   // Return the number of elements in the set  int getLength() {return len;}   void showset(); // display the set  bool isMember(char ch); // check for membership   Set operator +(char ch); // Add an element  Set operator -(char ch); // Remove an element   Set operator +(Set ob2); // set union  Set operator -(Set ob2); // set difference  }; // Find: return index of element specified by ch, else return -1 if not found int Set::find(char ch) {  int i;   //cout << "in find() ...
";  for (i=0; i < len; i++){   //cout << "i, members[i] = " << i << ", " << members[i];   if(members[i]==ch) return i;  }   return -1; }  // Show the set void Set::showset() {  cout << "{ ";  for (int i=0; i < len; i++)   cout << members[i] << " ";  cout << "}
"; } // Return true if ch is a member of the set, else false bool Set::isMember(char ch) {  if (find(ch) != -1) return true;  return false; }  // Add a unique element to a set Set Set::operator+(char ch) {  Set newset;  //cout << "len = " << len << '
'; // REMOVE WHEN WORKING  //cout << "MaxSize = " << MaxSize << '
'; // REMOVE WHEN WORKING  if (len == MaxSize) {   cout << "Set is full.
";   return *this; // return existing set  }   //cout << "duplicate existing set
";  newset = *this; // duplicate existing set   // see if element already exists // FINALLY FOUND THE BUG !!!!!!!!!!!!!, Must have copied from isMember above, it's == NOT != // if (find(ch) != -1) { // if not found, then add ... FINALLY FOUND THE BUG !!!!!!!!!!!!!  if (find(ch) == -1) { // if not found, then add // if (!isMember(ch)) { // if not found, then add, THIS WORKS TOO!   // add new element to new set   //cout << "adding new element ...
";   newset.members[newset.len] = ch;   newset.len++;  }  return newset; // return updated set  }  // Remove an element from the set Set Set::operator-(char ch) {  Set newset;  int i = find(ch); // i will be -1 if element not found   // Copy and compress the remaining elements  for (int j=0; j < len; j++)   if (j != i) newset = newset + members[j];   return newset;  }    // Set union Set Set::operator+(Set ob2) {  Set newset = *this;   // Add unique elements from second set  for (int i=0; i < ob2.len; i++)   newset = newset + ob2.members[i];   return newset; // return updated set }  // Set difference Set Set::operator-(Set ob2) {  Set newset = *this;   // Subtract elements from second set  for (int i=0; i < ob2.len; i++)   newset = newset - ob2.members[i];   return newset; // return updated set }    int main() {  // contruct 10 element empty Set  Set s1;  Set s2;  Set s3;    s1 = s1 + 'A';  cout << "getLength() = " << s1.getLength() << '
';  s1.showset();   // return 0; for debuging and stopping the program    s1 = s1 + 'B';  s1 = s1 + 'C';   cout << "s1 after adding A, B, and C: ";  s1.showset();   cout << '
';   cout << "Testing for membership using isMember().
";  if (s1.isMember('B'))   cout << "B is a member of s1
";  else   cout << "B is not a member of s1
";   if (s1.isMember('T'))   cout << "T is a member of s1
";  else   cout << "T is not a member of s1
";   cout << '
';   s1 = s1 - 'B';  cout << "s1 after s1 = s1 - 'B': ";  s1.showset();   s1 = s1 - 'A';  cout << "s1 after s1 = s1 - 'A': ";  s1.showset();   s1 = s1 - 'C';  cout << "s1 after s1 = s1 - 'C': ";  s1.showset();   cout << '
';   s1 = s1 + 'A';  s1 = s1 + 'B';  s1 = s1 + 'C';   cout << "s1 after adding A, B, and C: ";  s1.showset();   cout << '
';   s2 = s2 + 'A';  s2 = s2 + 'X';  s2 = s2 + 'W';   cout << "s2 after adding A, X, and W: ";  s2.showset();   cout << '
';   s3 = s1 + s2;  cout << "s3 after s3 = s1 + s2: ";  s3.showset();   s3 = s3 - s1;  cout << "s3 after s3 = s3 - s1: ";  s3.showset();   cout << '
';   cout << "s2 after s2 = s2 - s2: ";  s2 = s2 - s2; // clear s2  s2.showset();   cout << '
';   s2 = s2 + 'C';  s2 = s2 + 'B';  s2 = s2 + 'A';   cout << "s2 after adding C, B, and A: ";  s2.showset();    return 0;  }
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Sun Aug 09, 2009 3:23 pm

Chapter 10
Chapter 10: Inheritance, Virtual Functions, and Polymorphism This module discusses three features of C++ that directly relate to object-oriented programming: inheritance, virtual functions, and polymorphism. Inheritance is the feature that allows one class to inherit the characteristics of another. Built on the foundation of inheritance is the virtual function. The virtual function supports polymorphism, the “one interface, multiple methods” philosophy of object-oriented programming.
Completed August 09, 2009 Virtual Functions. OK, I've gone to the dark side ....
Code: Select all
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cctype> using namespace std;  // A class for 2-D objects. class TwoDShape {  // private  double width;  double height;   char name[20]; public:   // Default constructor  TwoDShape () {   width = height = 0;   strcpy(name, "unknown");  }   // Constructor for TwoDShape  TwoDShape (double w, double h, char *n) {   width = w;   height = h;   strcpy(name, n);  }   // Constructor object with equal width and height  TwoDShape (double x, char *n) {   width = height = x;   strcpy(name, n);  }    void showDim () {   cout << "Width and Height are " << width << " and " << height << "
";  }  // Accessor functions  double getWidth() { return width; }  double getheight() { return height; }  void setWidth(double w) { width = w; }  void setHeight(double h) { height = h; }  char *getName() { return name; }   // Add area() to TwoDShape and make it virtual  virtual double area() {   cout << "Error: area() must be overridden.
";    return 0.0;  }  };   // Triangle is derived from TwoDShape. class Triangle : public TwoDShape {  char style[20];  // now private public:    // Default constructor for Triangle.  // Automatically invokes default constructor of TwoDShape.  Triangle () {   strcpy(style, "unknown");  }   // Constructor for Triangle with 3 parameters  Triangle (char *str, double w, double h) : TwoDShape (w, h, "triangle") {   strcpy(style, str);  }   // Construct an isosceles triangle  Triangle (double x) : TwoDShape (x, "triangle") {   strcpy(style, "isosceles");  }   // This now overrides area() defined in TwoDShape  double area() {   return getWidth() * getheight() / 2;  }   void showStyle () {   cout << "Triangle is " << style << '
';  } };     // Extend Triangle. class ColorTriangle : public Triangle { // ColorTriangle inherits Triangle which inherits TwoDShape.  char color[20]; public:   ColorTriangle (char *clr, char *style, double w, double h) : Triangle (style, w, h) {   strcpy(color, clr);  }   // Display the color  void showColor() {   cout << "Color is " << color << '
';  }  };   // Rectangle is derived from TwoDShape. class Rectangle : public TwoDShape { public:   // Constructor for Rectangle  Rectangle (double w, double h) : TwoDShape (w, h, "rectangle") { // call the TwoDShape constructor  // Initialize the base class portion (the TwoDShape portion of Rectangle).  setWidth(w);  setHeight(h);  }   // Construct a square  Rectangle (double x) : TwoDShape (x, "rectangle") { }   bool isSquare() {   if(getWidth() == getheight()) return true;   return false;  }   // Another override of area()  double area() {   return getWidth() * getheight();  }  };     int main() {   // Declare an array of pointers to TwoDShape objects  TwoDShape *shapes[5];   shapes[0] = &Triangle("right", 8.0, 12.0);  shapes[1] = &Rectangle(10);  shapes[2] = &Rectangle(10, 4);  shapes[3] = &Triangle(7.0);  shapes[4] = &TwoDShape(10, 20, "generic");   for (int i=0; i < 5; i++) {   cout << "Object is " << shapes[i]->getName() << '
';    cout << "Area is " << shapes[i]->area() << '
';    cout << '
';   }    return 0;  } 
Two more chapters to go! I signed up for a community college class in almost three weeks.
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Re: July/August Challenge - C++ self teach

Postby Unity » Mon Aug 10, 2009 2:40 am

Good going ST. What subject will you be studying?
Unity
Mega Poster
Mega Poster
 
Posts: 7815
Joined: Fri Mar 19, 2004 11:01 pm

Re: July/August Challenge - C++ self teach

Postby Scenario Thinker » Mon Aug 10, 2009 6:41 am

Same subject, C++. I'm doing this self-teach so I won't be in the dark going into the class, but I feel the class will give me more problems to solve and practice. If I just self teach and don't use it, I'll lose it.
S.Thinker
....o
^/v
/>
User avatar
Scenario Thinker
Mega Poster
Mega Poster
 
Posts: 7327
Joined: Sun Mar 14, 2004 11:01 pm
Location: near Chicago

Next

Return to Monthly Challenges

Who is online

Users browsing this forum: No registered users and 1 guest