Sie sind auf Seite 1von 35

C++

Evoved from C

Varous Versons/Impementatons

ARM (Stroustrup)-->ANSI base

ANSI/ISO
The Language

An OOPL

Aso supports procedura paradgm

Typed Language

Tres compatbty wth C

More strcter type checkng than C

Man Features:

procedura

functon overoadng, operator overoadng

casses, abstract casses, mutpe


nhrtance, vsbty contro, mpementaton
nhertance, poymorphsm

Namespaces

Genercs (tempates)
Stricter on typing

W the beow compe n C++?


#ncude<stdo.h>
f(){}
strict type specification

Ths coud compe n eary C


man () {
prntf("a"); // comped n eary C
}
strict on typing ..

Return must return an nt,

man() must be decared as returnng nt


man () {
return;
}
-
Variable declarations
Varabe decaratons need not be ony at
the begnnng of the bock/functon
defnton
vod f(vod) {
nt ;
=++;
nt |=; // not |ust at the top of the functon body
}
-
Local Variables in for loop:
scope restricted

whch gets used n the end n the beow


code?
#ncude <ostream>
usng namespace std;
nt =10;
nt man () {
for (nt =0;<15;++) {
++;
};
cout << << "\n"; // whch ?
// try compng wth -fno-for-scope (.e. semantcs pror to so)
}
Null is 0, Null need not be
used

no need to ncude a brary defnng Nu


nt man () {
nt *p=0;
// nt *q=1; // w ths work?
}
Scope Resolution Operator

You can use varabes defned n outer


scope
#ncude <ostream>
usng namespace std;
nt x = 10;
nt man() {
nt x = 5;
cout << x << "\n";
cout << ::x << "\n";
}
constants

wrte ony once varabes


nt man() {
const nt x=10;
// x= 20; // w not be permtted
}
Pointers and Constant

The ob|ect s constant


const char *p;
p = "abcd\n";
cout << p;
// p|0| = 'x'; // not permtted
char const *q;
q = "abcd\n";
cout << q;
q = "efgh\n"; // the ponter tsef s not constant
Pointers and Constant

const nt c = 10;

constant nteger

const nt *pc = &c;

ponter to a constant nteger

const nt *const cpc = pc;

constant ponter to a constant nteger

const nt **ppp;

ponter to a ponter to a constant nteger



namespaces

usng unocks a namespace


namespace B {
nt p,q;
}
usng namespace B;
nt man () {
p = 10;
x = 20; <--- ?
}
anonymous namespaces

where's the usng decaraton?


namespace {
nt x = 10;
}
nt man () {
cout << x << "\n"; // w ths work?
}
-
anonymous namespaces ..

anonymous names are aready unocked


nsde the fe
namespace A {
nt x,y;
}
namespace B {
nt p,q;
}
usng namespace B;
nt man () {
p = 10;
x = 20;
}

namespaces ..

whch x n beow code?


namespace H {
nt x = 10;
}
nt man() {
nt x = 20;
usng namespace H;
cout << x << "\n"; //whch x?
{
nt &x = H::x;
cout << x << "\n"; //whch x?
}
}
filescope static deprecated

Anonymous Namespaces can be used


#ncude <ostream>
usng namespace std;
vod f(vod);
statc nt x;
//can be rewrtten as:
// namespace { nt x;}
namespace {nt y;}
nt z;
vod f(vod) { x++; y=x+1; z++; cout << x << " " << y << " " << z << "\n";}
using a namespace inside
another
namespace H {
nt x = 10;
}
namespace G {
nt y=20;
usng namespace H;
}
nt man() {
usng namespace G;
cout << x << "\n" << y << "\n"; //w ths work?
}
namespaces ...

w ths work?
namespace H {
nt x = 10;
nt y = 10;
}
namespace G {
nt y=20;
usng namespace H;
}
nt man() {
usng namespace G;
cout << x << "\n" << y << "\n"; //w ths work?
}
namespace aliasing
namespace A = B;
usng namespace A;
you can use aasng wth condtona
processng
C in C++

extern "C"
extern "C" {
#ncude <stdo.h>
}
nt man () {
prntf ("heo\n"); // w ths work?
}
Symbol __cplusplus

s defned for a c++ comper


#fdef __cpuspus // ans/so C++?
nt =10;
#endf
nt man () {
++; // w ths compe?
}
-
Header file usable by both C
and C++

w ths compe n C as we as C++?


#fdef __cpuspus
extern "C" {
#endf
#ncude <stdo.h>
#fdef __cpuspus
}
#endf
-
typedef not needed

use the struct drecty as a type


struct Rec {
nt ;
char |;
};
nt man () {
Rec r;
r.=9;
r.|='a';
}
Overloading of functions

sgnature dependent statcay


resovabe poymorphsm
nt d(nt ) {return ;}
char d(char ) {return ;}
nt man () {
nt x=10;
char c='x';
cout << d(x) << d (c) <<"\n";
}
overloading []() example

notce the reference


cass myArray {
nt *a;
nt MAX;
pubc:
myArray(nt sze) { a = new nt (sze); MAX =
sze;}
nt & operator ||(nt ) { // try removng '&'
f ( >=MAX) throw new OutOfBound();
ese return a||;
}
};
default arguments

defaut vaues to parameters


nt d(nt =30) {return ;}
char d(char ='r') {return ;}
nt d1(nt =30) {return ;}
char d1(char ) {return ;}
nt man () {
nt x=10;
char c='x';
cout << d(x) << d (c) <<"\n";
cout << d1() << "\n"; // w ths work?
}
parameters

o/p?
nt counter=0;
nt f() {
return counter
++;
}
nt x () { return 1;}
nt y () { return 2;}
nt z () { return 3;}
man () {
cout << x() << y() <<
z() << "\n";
cout << f() << f() <<
f() << "\n";
}
order of evaluation

o/p?
nt counter=0;
nt f() {
return counter ++;
}
vod h (nt p, nt q) {
cout << "p=" << p << " q="
<< q << "\n";
}
man () {
h( f(), f());
}
order or evaluation
(ex: nesting)

nested cas
nt counter=0;
nt f (nt p, nt q) {
return (p+q);
}
nt g(nt ) {
counter =;
cout << counter << end;
return counter;
};
man () {
cout << f( f ( f (1,g(9)),
g(7)),g(5)) << end;
}
Point of Declaration

what vaue to expect n |?


nt |=10;
nt man() {
nt |=|;
cout << | <<"\n"; //
what output?
}
more on scoping ..

whch |?
nt |=10;
nt man() {
nt =|,|;
| = 20;
cout << <<" " << | << "\n";
// what output?
}
-
explicit casting of void*
needed
nt =10;
vod *v;
v = &;
nt *|;
| = statc_cast<nt *>(v);
cout << *| << "\n";
}
virtual destructors

cass A{..} <--- cass has a destructor

cass B: pubc A{..} <--- has a destructor

A *a = new B();

deete a;

B *b=new B();

deete b;
Koenig Lookup

ookng up for defntons through


namespaces

Ordnary ookup - go through usng


decaratons, and foow scopng rues

Argument dependent ookup

even f you haven't used a namespace, but


f you have used a name assocated wth a
namespace, addtona ookup s possbe..

Das könnte Ihnen auch gefallen