Introducing C++


- Let’s begin our first look into C++

- We’ll begin with a broad overview of many of the features in C++

- Our goal is “get our feet wet” by looking briefly at some of the features of C++

- We will be studying each of these topics in depth in future study notes


    - C++ language was developed at Bell Laboratories by Bjarne Stroustrup
    - Bell also developed C language
    - In addition to C, BCPL and Simula67 languages also influenced C++

Learning C++
    - You already know a lot about C++!
    - C++ is a superset of C (includes all of C plus [or ++] new features)
    - C++ provides facilities for object-oriented programming
    - Major hurdle is not syntax, it's programming style and design

Our first C++ program
- Consider the following example C++ program


#include <stdio.h>

#include <string.h>


// A string class

class string



  char data[80];



  string( char *s = "" ) { copy( s ); }


  void copy( char *s ) { strcpy( data, s ); }

  void read( FILE *fptr );

  void write( FILE *fptr );


  void operator+=(string &s) { strcat( data, ); }

  friend void append( string &s );



void string::read( FILE *fptr )


  int slen;

  fgets( data, 80, fptr);

  slen = strlen(data) - 1;

  if( data[slen] == '\n' )

    data[slen] = 0;




void string::write( FILE *fptr )


  fprintf( stdout, "%s", data );



void append( string &s )


  string tempstr;;

  s += tempstr;





  string str;


  printf( "Enter a string\n" ); stdin );

  printf( "Enter string to append\n" );

  append( str );

  printf( "The converted string is: ");

  str.write( stdout );

  printf( "\n" );



     - Save this in a file with .C or .cpp extension and compile as follows

     - Note we’re now using the C++ compiler (g++)

jdoe@unix% g++ -o string string.C   

     - Execute the program as follows:

 jdoe@unix% string   

    - Note: we can include C language statements and C library functions
    - Let's briefly examine some of the new features
    - Goal is to get familiar with the syntax and design
    - We'll be examining each component in detail throughout semester

    - New syntax for comments
    - Can use both new and old C syntax

// String conversion program

/* String conversion program */

     - Facility for object-oriented programming

     - Combine data and functions in one entity
     - Data model + Data model operations
     - Combines data and operations

     - Classes are defined in a class definition below

class string
    char data[80];

    void copy( char *s) { strcpy(data,s); }
    string( char *s="") { copy(s); }
    void read( FILE *fptr );
    void write( FILE *fptr);
    void operator+=(string &s) { strcat(data,; }
    friend void append(string &s);

    - Defining new class type named string
    - Combines character array with operations to manipulate array
    - Classes used to declare or instantiate objects
    - Each component (data & functions) is called a class member
    - Classes provide for information hiding (private, public)

Note about structures
    - Can still use struct as in C
    - struct in C++ is actually a special case of a class
    - struct is a class with all public members by default
    - class has all private members by default

Member functions
    - Can access other members of the class
    - Can be called only by referring to the object in which it resides

void write( FILE *fptr );


    - Member function of class string
    - Serves as function prototype or declaration


void string::write( FILE *fptr )
    fprintf( fptr, "%s", data );


    - Attaches the class with the function by the scoping operator (::)
    - Allows the function to access other members (i.e. data)
    - Called differently than C functions...




    - Selects the object (str) and calls it's function (write)
    - Functions also referred to as methods
    - Calling an object function is also called message passing



Friend functions
    - Functions not tied to a class definition
    - Can be used to access private class members

    friend void append( string &s );

    - Note friend keyword indicating function type
    - Unlike member functions, objects must be passed
    - Provide a "bridge" between C and C++

Reference variables
    - An alias for another variable
    - Similar to pointers
    - Passed by address
    - Declared with & operator

friend void append( string &s );



Constructor and destructor functions


string(char *s="") { copy(s); }


    - Constructors called automatically whenever object declared
    - Destructors called when object is no longer needed


string str( "This is a test" );
string str;


    - Note default arguments (used with any type of function)

Inline functions
    - C++ provides the facility for inline functions to reduce function calling overhead

    - During compilation, code is inserted at the location of the call with appropriate variables renamed

    - A function that has more than a few statements should probably not be inline                                                                                   
    - The copying overhead each time the function is called would be too great

    - A special keyword (inline) is used to denote a function as inline as in the example below

#include <stdio.h>

inline int plus_one( int n ) { return n+1; }

    int i = 15;
    int j;

    j = plus_one(i);
    printf( "The value of j is %d\n", j );

    - When compiled, the statement

j = plus_one(i);

   is replaced with

j = i+1;

    - Small class member functions are good candidates for inline functions
   - Inline class member functions simply include the function immediately after the declaration

void copy( char *s ) { strcpy( data, s ); }




Overloaded functions and operators


s += tempstr;


     - Can use familiar operators we've used in C with objects
    - We overload operators to operate differently with objects
    - Besides its original, we associate other tasks with an operator
    - We can also overload functions to have more than one definition

Some other features...

Stream I/O operators


#include <iostream.h>

void main()
    int value;

    cout << "Input a number" << endl;
    cin >> value;
    cout << "The number is " << value << endl;

 - << operator replaces printf
 - cout replaces stdout
 - sends the string to standard output
 - cin designates input stream (standard input)

Type casting
    - Can treat it like a function call
    - Is useful to develop our own conversion functions

C:           i =  ( int )variable;

C++:       i =  int( variable_name );