1.124 Lecture 2 | 9/12/2000 |
By contrast, object-based programming focuses on the organization of software into a collection of components, called objects, that group together
The object-oriented programming paradigm goes a step beyond abstract data types by adding two new features: inheritance and polymorphism. We will talk about these ideas in depth later, but for now it will be sufficient to say that their purpose is to facilitate the management of objects that have similar characteristics. For example, squares, triangles and circles are all instances of shapes. Their common properties are that they all have a centroid and an area. Their common method might be that they all need to be displayed on the screen of a computer. Examples of languages that support the object-oriented paradigm are C++, Java and Smalltalk.
#include <stdio.h>
/* The HelloWorld procedure definition. */
void HelloWorld() {
printf("Hello World!\n");
}
/* The main program. */
int main() {
HelloWorld();
/* Execute the HelloWorld procedure. */
return 0;
/* Indicates successful completion of the program. */
}
It is important to understand the distinction between a class and an
object. A class is merely a template for creating one or more objects.
Our main program creates a single object named a based on the class
definition that we have provided. We then send the object a "print"
message by selecting and invoking the print() method using the .
operator. We are able to access the print() method in main()
because we have made it a public member function of the class.
#include <stdio.h>
// The HelloWorld class definition.
class HelloWorld {
public:
HelloWorld() {}
// Constructor.
~HelloWorld() {}
// Destructor.
void print() {
printf("Hello World!\n");
}
};
// Note that a semicolon is required here.
// The main progam.
int main() {
HelloWorld a;
// Create a HelloWorld object.
a.print();
// Send a "print" message to the object.
return 0;
}
/* C-style comments are also allowed in C++. */
// Alternative comment syntax that is only allowed in C++.
C++ built-in data types are similar to those found in C. The basic built-in types include
Built-in data type | Size in bytes |
---|---|
char, unsigned char | 1 |
short, unsigned short | 2 |
wchar_t, bool,
int, unsigned int, float |
4 |
double | 8 |
long double | 8 or 16 |
A literal constant is a constant value of some type. Examples
of literal constants are
Data type | Literal constant |
---|---|
char | 'a', '7' |
wchar_t | L'a', L'7' |
bool | true, false |
long int | 8L, 8l |
unsigned long int | 8UL, 8ul |
float | 2.718F, 2.718f |
double | 2.718, 1e-3 |
long double | 2.718L, 2.718l |
"Hello World!"
struct date {
int day;
int month;
int year;
void set_date(int d, int m, int y);
// Member functions only allowed in C++.
};
int main() {
struct date a; /* C-style definition.
*/
date a;
// Allowable C++ definition.
}
Pointer variables (or pointers) are a powerful concept that allow us to manipulate objects by their memory address rather than by their name. It is important to understand pointers clearly since they are used extensively in this course and in real world software.
A pointer must convey two pieces of information, both of which are necessary to access the object that it points to:
double *p;
p can now hold the memory address of a double object, such as d. We obtain the address of d by applying the address of operator, &d, and we then store it in p. Now that p contains a valid address, we can refer to the object d by applying the dereference operator, *p. Notice that we have used * in two different contexts, with different meanings in each case. The meaning of & also depends on the context in which it is used.
#include <stdio.h>
int main() {
double d;
// An double object.
double *p;
// A variable that is a pointer to an double.
p = &d; // Take the memory address of d and store it in p.
d = 7.0;
// Store a double precision number in d.
printf("The value of the object d is %lf\n",
d);
printf("The value of the object that p points
to is %lf\n", *p);
printf("The address of the object that p points
to is %u\n", p);
}
Here is the output from a trial run:
The value of the object d is 7.000000
The value of the object that p points to is 7.000000
The address of the object that p points to is 4026528296
As an added convenience, C++ provides reference types, which are an alternative way to use the functionality that pointers provide. A reference is just a nickname for existing storage.
The following example defines an integer object, i, and then it defines a reference variable, r, by the statement
int& r = i;
Be careful not to confuse this use of & with the address of operator. Also note that, unlike a pointer, a reference must be initialized at the time it is defined.
#include <stdio.h>
int main() {
int i = 0;
int& r = i; // Create a
reference to i.
i++;
printf("r = %d\n", r);
}
Explicit type conversion can be performed using a cast operator. The following code shows three alternative ways to explicitly convert an int to a float.
int main() {
int a;
float b;
a = 3;
b = (float)a;
/* C-style cast operator. */
b = float(a);
// Alternative type conversion notation allowed in C++.
b = static_cast<float>(a);
// A second alternative, allowed only in Standard C++.
}
The const keyword is used to designate storage whose contents cannot be changed. A const object must be initialized at the time it is defined.
const int i = 10; /* Allowed both in C and C++.
*/
const int j;
/* This is illegal. */
#include <stdio.h>
int main() {
int i, j; /* C requires variable
definitions to be at the top of a code block. */
for (i = 0; i < 5; i++) {
printf("Done with
C\n");
}
j = 10;
}
In the C++ version of the program, we can define the variables i and j when they are first used.
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
// In Standard C++, i is available anywhere within the for loop.
printf("Still learning
C++\n");
}
int j = 10;
}
1 + 4 * 3 / 2 == 7 && !0
is evaluated as
((1 + ((4 * 3) / 2)) == 7) && (!0)
Note that the right hand side of the logical AND operator is
only evaluated if the left hand side evaluates to true. (For
a table of operator precedence, see Lippman, Table 4.4.)
Here is an example of an inconsistent coding style. The curly
braces in the two for loops are aligned differently. The second
style is usually preferred because it is more compact and it avoids excessive
indentation.
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++)
{
printf("This convention aligns the curly braces.\n");
}
for (i =0; i < 5; i++) {
printf("This is a more
compact convention which aligns ");
printf("the closing brace
with the for statement.\n");
}
}