C++ – Classes and Objects, Part II

Continuation of Lesson 9 in Sam’s Teach Yourself C++ in One Hour a Day, 8th Edition

Constructors

A constructor is a special method that is run when an object is created. It lets the compiler know how the object will be built, or constructed. Constructors are useful for initializing member variables. You can also have multiple constructors, meaning they can be overloaded.

Basic syntax

cpp_class_constructor

cpp_class_constructor_2

Continue reading “C++ – Classes and Objects, Part II”

Advertisements

C++ – Classes and Objects, Part I

What I’m learning from Lesson 9 in Sam’s Teach Yourself C++ in One Hour a Day, 8th Edition

Object-oriented programming (OOP) is a different way of thinking about programs. Typically, beginner programmers are introduced to procedural programming — code is organized into different functions. The code can usually be read from the top to bottom, and the flow can be recognized.

With OOP, as the name suggests, you are encouraged to think of your data as different objects. For example, think of a human. A human is an object that has attributes, such as first name, last name, height, weight, etc. This human object can also carry out methods, or do things, such as speak, jump, eat, sleep, etc.

With this way of thinking about programming, you can then organize your code into different objects, each describing a specific part of your program and executing the logic piece by piece.

Continue reading “C++ – Classes and Objects, Part I”

Learning Markdown

Markdown is a simple markup language (similar to HTML) that is used to format the README.md files in GitHub. Since I’m planning on using GitHub more, I wanted to learn the basics of Markdown.

In my very first post, Learning Git & GitHub, part of the process was creating a very simple, barebones Markdown file.

I’m going to add some more text to this file and format it using Markdown. Then, I will use my new GIT knowledge to update the README.md file in my GitHub repo.

I watched a very brief introduction to Markdown from Brad Traversy on YouTube.

Continue reading “Learning Markdown”

C++ – References

I learned about references at the end of Lesson 8 in Sam’s Teach Yourself C++ in One Hour a Day, 8th Edition.

References are essentially aliases. To use a reference, an ampersand (&) is used on the l-value after the type. When a reference is defined, it takes on the value of the variable assigned to it. As a result, both the reference and the original variable can be used interchangeably. The value can  be update by changing either the original variable or the reference variable.

int x = 42;
int & intRef = x; // create reference to x
x = 30; // value of intRef is also 30
intRef = 19; // value of x is changed to 19, too

Continue reading “C++ – References”

C++ – Pointers, Part II

Currently going through Sam’s Teach Yourself C++ in One Hour a Day, 8th Edition from Safari Books Online.

In Lesson 8 I learned a few more things about pointers.

Dynamic Memory Allocation

Arrays are an example of static memory allocation. A set block of memory is reserved when it is defined. You cannot add more memory blocks, if needed. Nor can you shrink the allocated memory if you do not need as much. This is where dynamic memory allocation comes in.

Continue reading “C++ – Pointers, Part II”

C++ – Pointers

I’ve been learning C++ for the last month and a half. I do have some basic programming knowledge (studying Javascript, as well, for the last 8 months or so). Also, in college (10 years ago!) I took a C++ course, and I do recall many of the basics. However, I’ve learned that the language has gone through a few significant updates since then.

One of the aspects of C++ I remember struggling with a bit was pointers. So, I want to make sure and jot down some of the things I’m learning about them.

I referenced this website: http://www.cplusplus.com/doc/tutorial/pointers/

I may reference it again for other topics. I am going through another book on C++, and just reached the Pointers chapters, so I may have another post on this topic.

Here’s what I learned about pointers:

Gist

Essentially, the pointer is a variable that contains an address — it points to the address of another variable. From my understanding, pointers are useful since you can manipulate data simply by pointing to its address and dereferencing it, rather than having to pass the data itself onto the stack. Imagine having to pass a large value or an array onto the stack.

Continue reading “C++ – Pointers”