Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Programming in C++

Wenjing Ma
Email: mawe@cse.ohio-state.edu
Tel: 292-4634
CSE 459.22
 Time: 8:30 – 9:18 Tue
 Location: DL 266
 Office hours: Mon 2:30~4:30 pm, DL 788

 Webpage: http://www.cse.ohio-
state.edu/~mawe/459.22/
What is Programming?
 Take
A problem (Find the area of a rectangle)
 A set of data (length, width)
 A set of functions (area = length * width)
 Apply functions to data to solve the probl
em
Programming Concept Evolution

 Unstructured
 Procedural
 Object-Oriented
Procedural Programming

The main program coordinates calls to procedures and


hands over appropriate data as parameters.
Procedural Languages
 Usually there is a main function, which is aimed to solve t
he problem. In a function, arguments are passed to the
subfunctions, and results are returned.
 Examples: C, Pascal, Basic, Assembly language…
 For the rectangle area problem, the procedure to solve it
is:
int compute_area (int l, int w)
{
return ( l * w );
}
Object-Oriented Concept

Objects of the program interact by sending messages to


each other
Objects
 An object is an encapsulation of both functions and data (not o
ne or the other individually)
 Objects are the abstractions of real world entities
 Classes are data structures that define common properties or attributes
 Objects are instances of a class
 Objects have States
 Each property or attribute has a value at a particular time
 Objects have Operations
 associated set of operations called methods describe how to carry out
operations
 Objects have Messages
 request an object to carry out one of its operations by sending it a
message
 messages are the means by which we exchange data between objects
Object-Oriented Concept
 The rectangle area problem
 Define a class: Rect
 Data: width, length
 Functions: compute_area()
 An object: an instance of the class Rect
 To Solve the problem, create an object of Rec
t, and request this object to return the area of
the rectangle
Example Code
class Rect
{
private:
int width, length; main()
public: {
Rect (int w, int l) Rect rect1(3,5);
{ int x;
width = w; x=rect1.compute_area();
length = l; cout<<x<<endl;
} }
int compute_area()
{
return width*length;
}
}
Characteristics of OOPL
 Encapsulation: Combining data structure with actions
 Data structure: represents the properties, the states, or characteristics of
objects
 Actions: permissible behaviors that are controlled through the member
functions
Data hiding: Process of making certain data inaccessible
 Inheritance: Ability to derive new objects from old
ones
 permits objects of a more specific class to inherit the properties (data)
and behaviors (functions) of a more general/base class
 ability to define a hierarchical relationship between objects
 Polymorphism: Ability for different objects to interpret
functions differently
Encapsulation
class Circle class Triangle
{ {
private: private:
int radius int edgea, edgeb, edgec;
public: public:
Circle(int r); Triangle (int a, int b, int
c);
// The area of a circle
int compute_area(); // The area of a triangle
}; int compute_area();
};
Abstract Types
class Shape
{
public:
Shape();

// Calculate the area for


// this shape
virtual int compute_area() = 0;
};
Inheritance and Polymorphism
class Circle : public Shape class Triangle : public Shape
{ {
private: private:
int radius; int edgea, edgeb, edgec;
public: public:
Circle (int r);
Triangle (int a, int b, int c);
int compute_area();
int compute_area();
};
};

int sum_area(Shape s1, Shape s2) {


return s1.compute_area() + s2.compute_area();
// Example of polymorphism
}
Basic C++
 Inherit all ANSI C directives
 Inherit all C functions
 You don’t have to write OOP in C++. Bu
t C++ is a good language to do OOP.
Basic C++ Extension from C
 comments
/* You can still use the old comment style, */
/* but you must be very careful about mixing them */
// It's best to use this style for 1 line or partial lines
/* And use this style when your comment
consists of multiple lines */
 cin and cout (and #include <iostream.h>)
cout << “Hello";
char name[10];
cin >> name;
cout << “Hi, " << name << ", nice name." << endl;
cout << endl; // print a blank line

 declaring variables almost anywhere


// declare a variable when you need it
for (int k = 1; k < 5; k++)
{
char a=‘m’;
cout << k << a;
}
Basic C++ Extension from C (II)
 const
 In C,#define statements are handled by the pre
processor, so there is no type checking.
 In C++, the const specifier is interpreted by the
compiler, and type checking is applied.
 New data type
 Reference data type “&”. Much likes pointer
int ix; /* ix is "real" variable */
int & rx = ix; /* rx is "alias" for ix */
ix = 1; /* also rx == 1 */
rx = 2; /* also ix == 2 */
C++ - Advance Extension
 C++ allow function overloading
In C++, functions can use the same
names, within the same scope, if each
can be distinguished by its name and
signature
The signature specifies the number,
type, and order of the parameters
The name plus signature, then, uniquely
identifies a function
Take Home Message
 There are many different kinds of programming
paradigms, OOP is one among them.
 In OOP, programmers see the execution of the
program as a collection of dialoging objects.
 The main characteristics of OOPL include
encapsulation, inheritance, and polymorphism.
Not only OOPL can do OOP, but also others.

You might also like