You are on page 1of 8

Administrative Trivia

C++ Tour About me:


Piyush Kumar
Phone: 645-2355
For : COP 3330. Email: piyush@cs.fsu.edu
Object oriented Programming (Using C++) Office hours: Tuesday 4:30 to 5:30.
http://www.cs.fsu.edu/~piyush/teach/3330

TAs: Biswas Parajuli,


Daniel Mock,
More details on the course information sheet.
Piyush Kumar James Parsons

Administrative Trivia Your ID

TAs: You have been assigned an ID on the


Biswas Parajuli, (Sec: 15, 17) Blackboard. You should know your ID.

Daniel Mock, (Sec: 10, 16) You will use your FSU ID to setup your
bitbucket account.
James Parsons. (Sec: 2) https://bitbucket.org/

Office hours will be held in MCH 315A. Subject to change.


More details on the course information sheet.

Announcement Announcement

 First Quiz : Now  Blackboard Discussions already


 Revise your previous C/C++ material. setup.
- Your C++ background will be tested soon.  Make sure you submit the pre-req
 Your first assignment is online. form today before you leave.
 Will be due on Tuesday, 1/17/17.  Bitbucket setup of ssh keys.
 You are required to setup your  ssh-keygen –t rsa –C “fsu email”
bitbucket repository by this Thursday.
The submission will use the  Add it to bitbucket ssh-keys (without
blackboard account. adding \n)
 hg clone project1

1
Why learn C++?

C++ Tour  Ubiquitous


 Object Oriented
 Easier large scale projects
 Resuability
 High Performance

C++ Features When to use C++?

 Supports data security  Large projects


 Prevents accidents with data  System applications
 Graphics
 Helps code reuse.
 Data Structures
 Lets you use operators the way you
 Speed is an issue?
like.
 Changes in functionality required.
 Allows multiple functions/operators
 Need exceptions and error handling?
with the same name.
 Want to speed up your scripts?

When not to use C++? Important definitions

 Small system programs.  Algorithm: Ordered set of actions to


 Fast prototyping. accomplish a certain task.
 Program: Implementation of
 Web-based applications (Perl/Python)
algorithms.
 Compiler, function, library, bug.
 Variables, constants.
 Keywords (if, while, for,…)
 Data Types. (long, int, …)

2
Compiling/Running
Lynda.com
programs
 Single source file code:  Up and running with vi
 g++ -g –Wall –std=c++1y simple.cpp –o
simple  By David D. Levine
 Compilation / Editing Demo  1.5 hour video course in vim
 Editors: vim / xemacs  Your homework for this weekend is to
 Xemacs tutorial:
int main() { watch and practice vim
hanson.geog.udel.edu/cmg/Handouts/xema
return 0;
cs_tutorial.pdf

}
Vi/vim tutorial :
http://www.biochem.ucl.ac.uk/~mckenzie/vim
/tutorial.html

simple.cpp simple.cpp

int main() // Function Declaration  On Unix: echo $?


// Function body follows  On Windows: echo %ERRORLEVEL%

{ // Block of statements begins.

return 0;

} // Block ends.

System dependent

Simple.s (using g++ -O –S simple.cpp) Simple.s (using VC++ 05 compiler)


//{
.file “simple.cpp" 00411360 push ebp
.def ___main; .scl 2; .type 32; .endef 00411361 mov ebp,esp
.text 00411363 sub esp,0C0h
.align 2 00411369 push ebx
.p2align 4,,15 0041136A push esi
.globl _main 0041136B push edi
0041136C lea edi,[ebp-0C0h]
.def _main; .scl 2; .type 32; .endef
00411372 mov ecx,30h
_main: 00411377 mov eax,0CCCCCCCCh
pushl %ebp ; Save base pointer 0041137C rep stos dword ptr es:[edi]
movl $16, %eax // return 0;
movl %esp, %ebp ; Set up stack frame for debugger 0041137E xor eax,eax
subl $8, %esp ; Save space on the stack //}
andl $-16, %esp ; Align stack pointer 00411380 pop edi
call __alloca ; Platform dependent call 00411381 pop esi
00411382 pop ebx
call ___main ; Platform dependent call
00411383 mov esp,ebp
leave ; free space, pop ebp, esp… 00411385 pop ebp
xorl %eax, %eax ; zero eax 00411386 ret
ret ; return control to calling procedure.

3
G++ Compilation.
 preprocessing (to expand macros)


 Try “cpp simple.cpp > simple.i”
compilation (from source code to assembly language)
Introduction to C++
 Try “g++ -Wall –S simple.i”
 assembly (from assembly language to machine code)
 Try “as simple.s -o simple.o”
 linking (to create the final executable)
 Try “gcc simple.o”
 Equivalent to “ld -dynamic-linker /lib/ld-linux.so.2
/usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc-
lib/i686/3.3.1/crtbegin.o -L/usr/lib/gcc-lib/i686/3.3.1
hello.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc-
lib/i686/3.3.1/crtend.o /usr/lib/crtn.o”
$ file ./a.exe  Identify type of file generated (gcc on windows).
./a.exe: PE executable for MS Windows (console) Intel 80386 32-bit

Organization C++ is a superset of C.

 C++ = C + objects + …  Features present in C are also present in


 Main Theme : Objects C++
 Class Vs Object  C++ is a object oriented programming
 Data Hiding and Abstraction language ( C++ = c + objects + …)
 Encapsulation  OOP is about objects which contain data
 Inheritance and functions to manipulate that data.
 Operator and Function Overloading  A single unit that contains data and
 Polymorphism. functions that operate on the data is called
 Generic Programming an object.

Main Theme: objects OOP

 C++ enables you to focus on discrete  Based on concept of objects and


objects that contain both data and classes.
functions to manipulate that data.
 Objects: Represent entities with
 Instead of data and functions as
related state and behaviour
separate entities as was the case in
C.  Instances of a class
 Application = Collection of Objects.  Classes: Define common
 Makes complex programs easy to characteristics of similar objects.
code.

4
Objects/Classes Object / Class Example
Car Class
Attributes: Behavior:
 Objects are reusable self-contained Manufacturer Accelerate
programming modules with data and Model Break
Color Steer
functions. Engine Tune Up

 Classes are blue-print for objects with


common properties, attributes,
operations and behaviors.

The principal building blocks of OO programs are classes and objects.

Objects: Instantiations of classes.

Another Object / Class Fundamental building block


example of OOP: A Class
class Point {  A class defines a data type, much like
int _x, _y; // point coordinates a struct would be in C.
public: // begin interface section  A class is a source code for an object

void setX(const int val);


(hence it has both data+member
void setY(const int val); functions)
int getX() { return _x; }
int getY() { return _y; }
Attributes
}; …

Point tpoint; // defines an object tpoint class


Methods

Fundamental building block


Data Hiding
of OOP: A Class
 Objects contain information
 You can imagine that int is a class that
 Only part of the information contained
has member functions called
in the object might be presented to the
operator++, etc.
user.
 An object usually means an instance
 Rest is concealed.
of a class
 After the declaration int i; we say that
Engine
"i is an object of type int."
Interface

5
Data Hiding Data Hiding
 Internal dynamics are not visible to  Allows data to be accessed by certain
the user. functions class Point { // private
// concealed info
 Data hiding is done by using the
int _x, _y; // point coordinates
“private” keyword in the class public: // begin interface section

definition. void setX(const int val);


void setY(const int val);
int getX() { return _x; }
int getY() { return _y; }
Engine
};
Interface
Point tpoint; // defines an object tpoint

tpoint._x = 5; // ILLEGAL

Data Abstraction Data Encapsulation

 DA is a programming technique where  Combine lower level elements to form


one separates the interface from the a new higher level entity.
implementation  Grouping of attributes and behaviors
 Class designer worries about the to form an object.
interface
C++ vector type is an example of both data abstraction
 Programmers implement and Data encapsulation

String s; s.rfind(‘\’);

OOP : Inheritance Inheritance

 Many classes have common attributes.  Children inherit traits from their
 These classes can be arranged in a parents.
hierarchy.
 When a class is inherited all the
 Inheritance enables you to reuse code and
functions and data member are
add data and functionality without making
any changes to the existing code.
inherited, although not all of them will
 For example, objects can inherit
be accessible by the member
characteristics from other objects. functions of the derived class.

6
Inheritance Inheritance

 protected/public members of the base  Derived classes are specialized form


class are accessible to the derived of their base class.
class  Abstract class : A base class that is
 private members are not. undefined and unimplemented.
class vehicle {
protected:
char colorname[20]; class Car: public vehicle {
int number_of_wheels;
public:
protected:
vehicle(); char type_of_fuel;
~vehicle(); public:
void start(); Car();
void stop();
void run(); };
};
Subclass or derived class
Super class or Base class

Multiple definitions…? Operator Overloading

 C++ allows for the same function (or  C++ gives you the power to define operators
overloaded operator) to have multiple on user defined types.
defintions.  Example:
 Swap(int& I, int& j);  MyMatrix m = m1 + m2; // cant do this in C.
 Swap(string& I, string& j);  Overloading is a type of polymorphism.
 Swap(double& I, double& j);
 Not all operators can be overloaded in C++.
a.k.a. Function overloading.

Polymorphism Methods:
Draw() Polymorphism Methods:
Draw()
MyShape Erase() MyShape Erase()

2DShape 3DShape 2DShape 3DShape

Circle Square Triangle Sphere Cube Tetrahedron Circle Square Triangle Sphere Cube Tetrahedron

 Base Class MyShape establishes the  Overriding: When a child class extends the
functionality of its parent class.
common interface to anything  Polymorphism allows the programmer to
inherited from MyShape. implement different draw/erase methods for
derived classes.
 All shapes can be drawn and erased.
 Sphere and Cube have the same function
draw() with different functionality.

7
Polymorphism Methods:
Draw() Polymorphism
MyShape Erase()

2DShape 3DShape
 Using operators and functions in
different ways depending on what
Circle Square Triangle Sphere Cube Tetrahedron they are operating on is called
polymorphism.
 No matter what shape the object is,  Static
one can use the “draw” method to  Dynamic
draw it correctly.
 These concepts builds upon
encapsulation and inheritance.

Generic Programming

 Type independent code


 Write code for one generic type and
use it for multiple types.
Without Generic Programming:
void swap(int & x, int & y) { int tmp = x; x = y; y = tmp; }
void swap(long & x, long & y){ long tmp = x; x = y; y = tmp; }

With Generic Programming


template <typename T>
void swap(T & x, T & y) { T tmp = x; x = y; y = tmp; }

You might also like