Documente Academic
Documente Profesional
Documente Cultură
• Derivare şi parametrizare
• Polimorfism
– suprascriere -> legare statică
• exemplu
– funcţii virtuale -> legare dinamică
• exemplu
• static_cast, dinamic_cast
• Operatorul typeid
• Ierarhia de clase ios
• Fișiere C++
Popescu Ion
Student Angelescu Sorin
Profesor Marinescu Pavel
Angelescu Sorin
Marinescu Pavel
Popescu Ion
Angelescu Sorin
Marinescu Pavel
Popescu Ion
Student Angelescu Sorin
Profesor Marinescu Pavel
Popescu Ion
Student Angelescu Sorin
Profesor Marinescu Pavel
• Se obţine:
– 32 dacă semneaza() nu este virtuală
– 36 dacă semneaza() este virtuală
void figura::compute_area()
{area = 0.0;}
void process_figure(figura&);
figura* my_fig; // pointer la o figura
int main () {
try {
CBase * pba = new CDerived;
CBase * pbb = new CBase;
CDerived * pd;
pd = dynamic_cast<CDerived*>(pba);
if (pd==0) cout << "Null pointer on first type-cast" << endl;
pd = dynamic_cast<CDerived*>(pbb);
if (pd==0) cout << "Null pointer on second type-cast" << endl;
// const_cast
#include <iostream>
using namespace std;
int main () {
const char * c = “exemplu ";
print ( const_cast<char *> (c) );
return 0;
}
• Syntaxa
typeid( type-id )
typeid( expresie )
int main () {
int *a,b;
a=0; b=0;
if (typeid(a) != typeid(b))
{
cout << "a and b are of different types:\n";
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
}
return 0;
}
int main () {
try {
CBase* a = new CBase;
CBase* b = new CDerived;
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
cout << "*a is: " << typeid(*a).name() << '\n';
cout << "*b is: " << typeid(*b).name() << '\n';
} catch (exception& e) { cout << "Exception: " <<
e.what() << endl; }
return 0;
}
ios<>
istream<>
ostream<>
istringstream<> ostringstream<>
fstream<> stringstream<>
streambuf<>
filebuf<> stringbuf<>
/*
10 16 24
12 20 30
a 10 18
Introdu 3 intregi:
42 11 12a
42 17 298
*/
• Metode:
bool is_open() const;
void open(const char *s,
ios_base::openmode mode = ios_base::in);
void close();
int main () {
string line;
ifstream myfile ("file.cpp");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}else
cout << "Unable to open file";
return 0;
}
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open()){
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}
explicit basic_stringstream(const
basic_string<E,T,A>& x, ios_base::openmode mode =
ios_base::in | ios_base::out);
int main()
{
stringstream s("This is initial string.");
// get string
string str = s.str();
cout << str << endl;
int i;
double d;
s >> str >> i >> d;
cout << str << " " << i << " " << d;
return 0;
}