







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main topics in this course are object-orientation, objects and classes, overloading, inheritance, polymorphism, generic programming, exception handling, introduction to design patterns. This lecture includes: Member, Template, Float, Return, Class, Vector, Virtual, Const, Private, Public, Int
Typology: Slides
1 / 13
This page cannot be seen from the preview
Don't miss anything!








template
template
T real, imag; T real, imag;
public: public:
// Complex
// Complex
Complex( T r, T im ) :
Complex( T r, T im ) :
real(r), imag(im) {}
real(r), imag(im) {}
// Complex
Complex(const Complex
Complex(const Complex
real( c.real ), imag( c.imag ) {}
real( c.real ), imag( c.imag ) {}
}; }; docsity.com
int main() { int main() {
Complex< float > fc( 0, 0 ); Complex< float > fc( 0, 0 );
Complex< double > dc = fc; // OK
Complex< double > dc = fc; // OK
return 0;
return 0;
class Complex class Complex
float float real, imag;real, imag;
public:
public:
Complex(
Complex( float
float r,
r, float
float im ) :
im ) :
real(r), imag(im) {}
real(r), imag(im) {}
// No Copy Constructor // No Copy Constructor
template<>
template<>
class Vector< char* > { class Vector< char* > {
private:
private:
int size; int size;
char** ptr;
char** ptr;
public: public:
// Vector< char* >( int = 10 );
// Vector< char* >( int = 10 );
Vector( int = 10 ); Vector( int = 10 );
Vector( const Vector< char* >& );
Vector( const Vector< char* >& );
virtual ~Vector(); virtual ~Vector();
docsity.com
template<>
template<>
Vector<char>::Vector(int s) {Vector<char>::Vector(int s) {
size = s;
size = s;
if ( size != 0 ) { if ( size != 0 ) {
ptr = new char*[size];
ptr = new char*[size];
for (int i = 0; i < size; i++)for (int i = 0; i < size; i++)
ptr[i] = 0;ptr[i] = 0;
else else
ptr = 0;ptr = 0;
template<> template<>
int Vector<char>::getSize() const { int Vector<char>::getSize() const {
return size;
return size;
size = right.size; size = right.size;
if ( size == 0 ) { if ( size == 0 ) {
ptr = 0;
ptr = 0;
return *this;
return *this;
ptr = new char[size]; ptr = new char[size];
int main() {
int main() {
Vector< char* > sv1( 2 ); Vector< char* > sv1( 2 );
sv1[0] =
sv1[0] = “
Aamir
Aamir ”
; // Error
; // Error
sv1.insert( "Aamir", 0 ); sv1.insert( "Aamir", 0 );
sv1.insert( "Nasir", 1 );
sv1.insert( "Nasir", 1 );
Vector< char* > sv2( sv1 ); Vector< char* > sv2( sv1 );
Vector< char* > sv3( 2 );
Vector< char* > sv3( 2 );
sv3 = sv1; sv3 = sv1;
return 0;
return 0;
docsity.com