c_++
c_++
• C is a Procedural Language :
“Decide which procedures you want; use the best algorithms you can find”
Basics of C++
• Structure of a C++ program
• Constants
• Operators
int main ()
{
cout << “Hello Cs169 / Fall 2009 Students“;
return 0;
}
Main portion of 4
program.
Contains C++
statements.
Variables and Data Types
• Identifiers:
– Combination of letters, digits, and underscores
– Variable names should start with letter or digit
Fundamental DataTypes
Initialization of Variables
Two possibilities:
Initialization of Variables
// initialization of variables
#include <iostream.h>
int main ()
{
int x=5;
int y(2);
int result;
x = x + 3;
result = x - y
cout << result;
return 0;
}
Strings
Example
// my first string
#include <iostream>
#include <string>
using namespace std;
int main () {
string mystring;
mystring = “Hello Cs169, Fall 2009";
cout << mystring << endl;
mystring = “Hello Cs169, I wish you a great semester";
cout << mystring << endl;
return 0;
}
Arithmetic Operators
Logical Operators ( !, &&, || )
Basic C++ I/O
• “iostream” C++ library
• Cout
cout << “Hello Cs169”; //Print Hello Cs169
cout << 120; // Print 120 on the screen
cout << x; // print the content of x in the screen
• Cin
int age;
cin >> age;
cout << age
cin >> a >> b; is equivalent to cin>>a; cin>>b;
Control structures
Switch Statement
switch (x) { \
case 1:
cout << "x is 1";
break;
case 2:
cout << "x is 2";
break;
default:
cout << "value of x unknown";
}
Functions
• We can use functions to achieve structured
programming
Int add(int a, int b){
a = a+b; return (a);
}
Passing Parameters
• Call By Value
– This is what we usually do, we pass a function
the value of a variable
• Call By Reference
– Instead of the value we will pass a pointer to
the variable. If we modify the passed variable
the change will be seen by the caller
– We use ‘&parm’ to show that we are passing
the variable at the memory location of parm
Agenda
Session 1
4. Control Structures
Structures
Functions
Arrays
• Intialization
– int numbers[5]={0,1,2,3,4,};
• Accessing
– num2 =numbers[1];
– numbers[0]=99;
• Passing arrays as parameters
– Declaration: int add(int numarray[])
– Call:
int array[]={1,2,3};
add(array);
Arrays
Example
Pointers
Sizeof()
• The Sizeof() function is used to determine
how many bytes of a data type during
compilation.
• Ex:
sizeof(float) equals 4
float array[10];
sizeof(array) equals 4*10 which is40
Dynamic Memory
• Dynamic Memory allows us to determine
and allocate memory to variables and data
structures at ‘run time’.
• C++ uses new and delete;
– pointer = new type;
• New returns a pointer to the allocated memory
– delete pointer;
• Frees up the memory that was allocated
Dynamic Memory
Example
Structs
• Similar to records in Ada.
struct person_t{
char fname[20];
char lname[20];
int age;
}person1, person2;
• Here we are declaring person1 and person2 as type
person_t.
• By convention we use the _t
Pointers to Structs
• We can point to a struct like other structures.
Person_t* person1Ptr;
person1Ptr = &person1;
• We can no longer use the ‘.’ to access the
members in the struct we are pointing to.
– The ‘->’ is used
Cout << person1Ptr->fname;
– Element fname of structed pointed by person1Ptr
– Same as *(person1Ptr.fname);
C++ Classes
Example
Constructors
Example
mystring::mystring(char *c)
{
size = strlen(c);
str = new char[s+1];
strcpy(s,c);
}
mystring::~mystring()
{
delete []str;
}
Copy Constructor
Example
class myrectangle {
private:
float height;
float width;
int x;
int y;
public:
rectangle(float, float); // constructor
rectangle(const myrectangle&); // copy constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
};
Importance of a copy
constructors
• In the absence of a copy constructor, the
C++ compiler builds a default copy
constructor for each class which is doing a
memberwise copy between objects.
• Default copy constructors work fine unless
the class contains pointer data members ...
why???
void main()
{
string str1("George");
string str2 = str1; // default copy constructor
str2.copy("Mary");
Inheritance
• Objects are often defined in terms of
hierarchical classes with a base class and
one or more levels of classes that inherit
from the classes that are above it in the
hierarchy.
• For instance, graphics objects might be
defined as follows:
Inheritance
class A : base class access specifier B
{
member access specifier(s):
...
member data and member function(s);
...
}
Valid access specifiers include public, private,
and protected
Public Inheritance
class A : public B
{ // Class A now inherits the members of Class B
// with no change in the “access specifier” for
} // the inherited members
Inheritance (continued)
class Shape
{
public:
int GetColor ( ) ;
protected: // so derived classes can access it
int color;
};
class Two_D : public Shape
{
// put members specific to 2D shapes here
};
class Three_D : public Shape
{
// put members specific to 3D shapes here
};
Operator Overloading
• Operator overloading means that the operators:
Friends
• Basically, when you declare something as a friend,
you give it access to your private data members.
• This is useful for a lot of things – for very
interrelated classes, it more efficient (faster) than
using tons of get/set member function calls, and
they increase encapsulation by allowing more
freedom is design options.
Friends
• A class doesn't control the scope of friend
functions so friend function declarations are
usually written at the beginning of a .h file. Public
and private don't apply to them.
Friends
Example
Polymorphism
Compile-Time Binding vs.
Run-Time Binding
• A function’s name is associated with an entry
point, the starting address of the code that
implements the function
• Compile-time binding lets the compiler
determines what code is to be executed when a
function name is invoked
• Run-time binding is the binding of a function’s
name to an entry point when the program is
running
49
Polymorphism
Example
50