Lecture 1 - An Overview of C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Lecture One

An overview of C++

Ref: Herbert Schildt, Teach Yourself C++, Third Edn (Chapter 1)

© Dr. M. Mahfuzul Islam


Professor, Dept. of CSE, BUET
Object Oriented Programming (OOP)
 Emphasis is on data rather than procedure
 Programs are divided into classes. Instance of a class is called object.
 Data and functions are build around objects
 Data doesn’t flow freely around the system
 Data is hidden, no access from external functions
 Data of an object can be accessed only by the functions associated with
that object
 Objects communicate with each other through functions
 new data and functions can be easily added.

Example: C++, Java, Smalltalk


Features of OOP
Three Key Features of OOP
 Wrap up data and functions/methods together
Encapsulation  Insulation of data from direct access by the program –
data hiding

One Interface, multiple methods


 Function overloading:
(1) Use a single name to multiple methods;
Polymorphism
(2) different number and types of arguments.
 Operator overloading:
Use of single operator for different types of operands

 One class inherits the properties of another class


Inheritance  provide hierarchical classifications
 Permits reuse of common code and data

 Representing essential features without details


 Class defines a list of abstract attributes (data Abstraction
members) and methods to operate on these attributes
Encapsulation
 Wrap up data and functions/methods together
 Insulation of data from direct access by the program – data hiding
#include <iostream>
using namespace std;
class myclass {
int a;
public: method
myclass(); // constructor data
int geta() { return a;}
}; method data
myclass::myclass(){ data Class
cout << “In constructor\n”; method
a = 10;
data
}
int main(){
myclass ob;

cout << a; \\ wrong -Error Interface


cout << ob.geta(); \\ OK (Public method/data)
return 0;
}
Polymorphism
One Interface, multiple methods
 Function overloading:
(1) Use a single name to multiple methods;
(2) different number and types of arguments.
 Operator overloading:
Use of single operator for different types of operands
#include <iostream> date::date(char *str){
#include <cstdio> sscanf(str, “%d%*c%d%*c%d; &day,
using namespace std; &month, &year);
class date { }
int month, day, year; int main() {
public: date sdate(“31/12/99”);
date(char *str); date idate(31, 12, 99);
date( int d, int m, int y) ){
day = d; sdate.show();
month = m; idate.show();
year = y; return 0;
} }
void show(){
cout << day << ‘/’ << month << ‘/’ ; Operator overloading:
cout << year << ‘\n’; a = 4 + 6;
}
}; ob1 = ob2 + ob3;
Inheritance
 One class inherits the properties of another class
Bird
 provide hierarchical classifications
Attributes
 Permits reuse of common code and data Feathers
Lay eggs
#include <iostream>
using namespace std; Flying Nonflying
Bird Bird
class base {
Attributes Attributes
int x;
… …
public:
void setx(int n) { x = n; }
Robin Swallow Penguin Kiwi
void showx() { cout << x << ‘\n’; }
int getx(){ return x; } Attributes Attributes Attributes Attributes
}; … … … …

class derived: public base { int main(){


int y; derived ob;
public:
void sety(int n) { y = n; } ob.setx(10);
void showy() { ob.sety(20);
cout << y << ‘\n’; ob.showx();
cout << y+getx() <<‘\n’; ob.showy();
} return 0;
}; }
Abstraction
Abstract Class and Method:
Abstract class is a superclass without a complete implementation of every method.
 There can be no objects of an abstract class.
 Abstract can be used to create object references.
Abstract method refers to subclasser responsibility to override it, otherwise, it will
report a warning message.
 Constructor and static method cannot be Abstract.
abstract class Figure {
double dim1, dim2; class Dispatch {
Figure(double a, double b){ dim1 = a; dim2 = b;} public static void main(String args[]){
abstract double area(); Rectangle r = new Rectangle(4,5);
} Triangle t = new Triangle(4, 3);
class Rectangle extends Figure { Figure figref;
Java Code

Rectangle(double a, double b) {supper(a, b);}


double area(){ return dim1*dim2;} figref = r;
} System.out.println(“area: “+ figref.area());
class Traingle extends Figure { figref = t;
Triangle(double a, double b) {supper(a, b);} System.out.println(“area: “+ figref.area());
double area(){ return 0.5*dim1*dim2;} }
} }
Programming with C++
Two versions of C++:
Old version of C++: New version of C++:
#include <iostream.h> #include <iostream>
using namespace std;
int main(){
/* program code */ int main(){
return 0; /* program code */
} return 0;
}
 Includes filename
 Includes stream which is
mapped to file by compiler
Filename used in File stream used in
Old Version New Version Bjarne Stroustrup
iostream.h iostream (1979)
string.h cstring
math.h cmath
graphics.h cgraphics
C/C++ I/O
General form of C++ Console I/O
 Input Command: cin >> variable;
 Output Command: cout << expression;

C I/O C++ I/O


a) Input Statements
scanf(“%s”, strName); cin >> strName;
scanf(“%d”, &iCount); cin >> iCount;
scanf(“%f”, &fValue); cin >> fValue;
scanf(“%d %d %d”, &day, &month, &year); cin >> day >> month >> year;
b) Output Statements
printf(“%s%c%s%c”, “Hello”, ‘ ’, “World”, ‘!’); cout << “Hello” << ‘ ‘ << “World” << ‘!’;
printf(“Value of iCount is: %d”, iCount); cout << “Value of iCount is: ” << iCount;
printf(“Enter day, month, year”); cout << “Enter day, month, year: ”;
Namespaces
A namespace is a declarative region that localizes the names of
identifiers to avoid name collisions.
The contents of new-style headers are placed in the std namespace.
A newly created class, function or global variable can put in-
(1) an existing namespace;
(2) a new namespace; and
(3) unnamed namespace.
#include <iostream>
#include <iostream> using namespace std;
int main() { int main() {
char str[16]; char str[16];
std::cout << “Enter a string: ”; cout << “Enter a string: ”;
std::cin >> str; cin >> str;
std::cout << “String: ” << str; cout << “String: ” << str;
return 0; return 0;
} }
Scope Resolution Operator (::)
Scope Resolution Operator is used for two purposes:

To access a hidden global


variable
#include <iostream> To access a hidden member class or member
variable with a class
int count = 0;
#include <iostream>
int main(void) { using namespace std;
int count = 0;
::count = 1; // set global count to 1 class X {
count = 2; // set local count to 2 public:
static int count;
return 0; };
}
int X::count = 10; // define static data member

int main () {
cout << X::count << endl; // use static member of class X
}
C++ Comments

 Multi-line comments
/* one or more lines of comments */

 Single line comments


// …
Some differences between C and C++
SL# Area C C++
1. Empty parameter void is mandatory. void is optional.
list char f1(void); char f1();
2. Function Function prototype is optional but All functions must be
prototype recommended. prototyped.
3. Returning a value A non-void function in not If a function is declared as
required to actually return a value. returning a value, it must
If it doesn’t, a garbage value is return a value.
returned.  C++ has dropped the
 “Default-to-int” rule: If a “default-to-int” rule.
function does not explicitly specify
the return type, an integer return
type is assumed.
4. Local variable Local variables are declared at the Local variables can be
declaration start of a block, prior to any declared anywhere.
action statement.
5. bool data type - C++ defines the bool data
type and also keywords
true and false.

You might also like