Newformat for SPC UNIT 2

download Newformat for SPC UNIT 2

of 21

Transcript of Newformat for SPC UNIT 2

  • 7/31/2019 Newformat for SPC UNIT 2

    1/21

    DAY: 1 UNIT: II TOPIC: CONSTRUCTORCONCEPT DESCRIPTION QUESTIONSDEFINITION

    TYPES

    DEFINITION

    (Default

    constructor)

    Syntax

    Example

    A constructor is a special member function whose task is to initialize the objects of its

    class. It is special because its name is same as class name. The constructor is invokedwhenever an object of its associated class is created. It is called constructor because it

    constructs the value of data members of the class.

    Default constructor or no argument constructor Parameterized constructor

    Copy constructor

    Dynamic constructor

    Default constructor or no argument constructor

    Constructor is a special member function

    It enables an object to initialize itself when it is created

    It is also known as automatic initialization of objects

    It constructs the values of data members of a class Constructor name will be same as class name

    class default_constructor

    { public:

    default_constructor()

    {------}

    };

    class integer

    { int m,n;

    public:

    integer(void) //constructor declared

    1. Explain the various types of

    constructors.

    2. Explain the various types of

    constructors with example.

    1

  • 7/31/2019 Newformat for SPC UNIT 2

    2/21

    DEFINITION

    (Parameterized

    constructor)

    Syntax:

    };

    Integer :: integer(void)

    { m=0,n=0; }

    void main()

    { Integer I; }

    Parameterized constructor

    A constructor which takes the parameter or arguments are called as

    Parameterized constructor

    The constructor integer() may be modified to take arguments as below

    Then a constructor function has been parameterized the object declaration

    statement such as Integer int1;

    It will not work. We must pass the initial values as arguments to the constructorfunction when an object is declared.

    Integer int1=integer(0,100); //explicitly

    Integer int1(0,100); //implicitly

    By calling the constructor explicitly

    By calling the constructor implicitly

    class parameterized_constructor

    {

    public:

    parameterized_constructor(int,int)

    2

  • 7/31/2019 Newformat for SPC UNIT 2

    3/21

    Example

    { }

    };

    #include

    class integer

    { int m,n;

    public:

    integer(int,int);

    void display(void)

    { cout

  • 7/31/2019 Newformat for SPC UNIT 2

    4/21

    Output

    DEFINITION

    (Copy

    constructor)

    Syntax

    }

    Object1 m=0 n=100

    Object2 M=25 N=75

    Copy constructor

    A copy constructor is used to declare and initialize an object from another object

    integer l2(l1);

    It would define the object l2 and at the same time initialize it to the values of l1

    integer l2=l1;

    The process of initializing through a copy constructor is known as Copy initialization

    l2=l1;

    It will not invoke the copy constructor.

    If l1 & l2 are objects, it assigns the values of l1 and l2 member by member.

    A copy constructor takes a reference to an object of the same class as itself as an

    argument

    class copy_constructor

    {

    public:

    copy_constructor(copy_constructor &x)

    { }

    4

  • 7/31/2019 Newformat for SPC UNIT 2

    5/21

    Example

    };

    #include

    class code

    { int id;

    public:

    code(int a) //constructor

    { id=a; }

    code(code &x) //copy constructor

    { id=x.id; //copy in the value }

    void display(void)

    { cout

  • 7/31/2019 Newformat for SPC UNIT 2

    6/21

    Output

    DAY: 2

    cout

  • 7/31/2019 Newformat for SPC UNIT 2

    7/21

    It takes any number of

    arguments

    It takes no arguments

    It can be overloaded It cannot be overloaded

    A class can have multiple constructor Only one destructor function for a class ispossible

    Constructor name and the class nameshould be the same

    Eg:

    Class student

    { public:

    Student( );//constructor

    };

    Destructor name and class name should bethe same but it is prefixed by ~

    Eg:

    Class student

    { public:

    ~student( );

    }

    It cannot be declared as virtual function It can be a virtual function

    It behaves like a new operator It behaves like a delete operator

    DAY:3 UNIT:2 TOPIC:OPERATOR

    OVERLOADINGCONCEPT DESCRIPTION QUESTIONSDEFINITION The mechanism of giving special meaning to an existing operator is known as operator

    over loading.1. Define operator overloading.

    Explain with an example.

    7

  • 7/31/2019 Newformat for SPC UNIT 2

    8/21

    RULES

    SYNTAX

    Program

    Rules for overloading an operator

    Only existing operators can be overloaded.

    The overloaded operator must have at least one operand that is of user

    defined data type.

    The basic meaning of the operator should not be changed. That is, if theoperator to be overloaded to is + operator then it should be used only for

    addition operation, not for other arithmetic or any other purposes.

    Overloaded operators follow the syntax rules of the original operators. They

    cannot be overridden.

    Except the function call operator ( ), no other operator can have a default

    argument

    Some of the operators cannot be simply overloaded.

    Operators that cannot be overloaded.

    The operators which cannot be overloaded are:

    The dot operator for member access

    The dereference member to class operator .*

    Scope resolution operator::

    Size operator sizeof

    Conditional operator ?: Casting operators static_cast, dynamic_cast, reinterpret_cast,

    const_cast

    # and ## tokens for macro preprocessors

    The syntax for operator function is,

    Return type Operator Operator-symbol(arglist)

    2. Explain operator overloading.

    List down the characteristics of

    operator overloading

    3. List down the rules for

    overloading an operator

    4. Write a program to find thesum of two complex numbers

    using operator overloading.

    8

  • 7/31/2019 Newformat for SPC UNIT 2

    9/21

    { Function body }

    #include

    #include

    class complex

    {

    float real;

    float imag;

    public:

    complex(float tempreal=0, float tempimag=0);

    { real=tempreal; imag=tempimag; }

    complex add(complex comp1)

    {

    float tempreal;

    float tempimag;

    tempreal=real+comp2.real;

    tempiamg=imag+comp2.imag;

    return complex(tempreal,tempimag);

    }

    9

  • 7/31/2019 Newformat for SPC UNIT 2

    10/21

    complex operator+(complex comp2)

    {

    float tempreal;

    float tempimag;

    tempreal=real+comp2.real;

    tempiamg=imag+comp2.imag;

    return complex(tempreal,tempimag);

    }

    void display()

    {

    cout

  • 7/31/2019 Newformat for SPC UNIT 2

    11/21

    compresult2.display()

    }

    DAY:4 UNIT: II TOPIC: TYPE CONVERSIONCONCEPT DESCRIPTION QUESTIONSTYPES

    SYNTAX

    CONDITIONS

    There are three types of conversions.

    Conversion from built-in data type to an object

    Conversion from object to a built in data type

    Conversion from one object to another object type

    Any conversion involving a class is defined either

    by a conversion constructoe or

    by a conversion operator functionA conversion operator function performs conversion in the opposite direction , that

    is it converts an object of the current class to another type.

    General form:

    Operator type-name

    { Function body }

    A conversion operator function is a function that satisfies the following conditions

    It must be a member function

    It must not specify any return type

    It must not have any arguments

    Conversion from built-in data type to object

    Conversion from basic data type to object type can be done with the help of

    1. Explain in brieflyabout TypeConversion withexamples

    11

  • 7/31/2019 Newformat for SPC UNIT 2

    12/21

    Example:1

    constructors.

    Constructors takes a single argument whose type is to be converted.

    #include

    #include

    class sample

    { int x;

    public:

    sample()

    { }

    sample(int a)

    { x=a; }

    void print()

    { cout

  • 7/31/2019 Newformat for SPC UNIT 2

    13/21

    Example:2

    Conversion from object to built-in datatype

    Using conversion operator function, conversion from object of a class to a built-in

    data type can be done.

    #include

    #include

    class sample

    {

    int x;

    public:

    sample(int a)

    { x=a; }

    operator int()

    { return x; }

    };

    void main()

    {

    sample s1(10);

    int a =s1;

    cout

  • 7/31/2019 Newformat for SPC UNIT 2

    14/21

    Example:3

    be invoked explicitly as

    int a =(int)s1;

    Or

    int a int(s1);

    The compiler invokes the appropriate conversion function . The compiler looks foran overloaded operator =.

    Conversion from one object to another object (

    We can convert from one type of an object into another type of object using either

    constructors or conversion operator function.

    #include (

    #include

    class sample1

    { private:

    int x;

    public:

    sample1(int a)

    { x=a; }

    int geta()

    { return x; }

    };

    class sample2

    14

  • 7/31/2019 Newformat for SPC UNIT 2

    15/21

    { private:

    int y;

    public:

    sample2()

    { }

    sample2(sample1 s1)

    { y=s1.geta(); }

    void print()

    { cout

  • 7/31/2019 Newformat for SPC UNIT 2

    16/21

    We can use friends as operator function

    Friend is a violation of principle of information hiding and it is to be avoided asfar as possible

    Member functions cannot choose the first argument and it is always theinvoking object

    There are two cases where it is really important to use friend functions

    #include

    class matrix

    {

    int element[3][3];

    public:matrix()

    }; matrix(int tempmatrix[3][3])

    for(inti=0;i

  • 7/31/2019 Newformat for SPC UNIT 2

    17/21

    cin>>element[i][j];

    }

    void display()

    {

    for(int i=0;i

  • 7/31/2019 Newformat for SPC UNIT 2

    18/21

    return matrix(tempmatrix.element);

    }

    matrix operator*(int multiplier,matrix tempmatrix)

    {

    for(int i=0;i

  • 7/31/2019 Newformat for SPC UNIT 2

    19/21

    m4.display();

    }

    DAY: 6 UNIT: II TOPIC: DESTRUCTORS

    CONCEPT DESCRIPTION QUESTIONS

    DEFINITION

    SYNTAX

    CHARACTERISTICS

    Destructors are usually used to deallocate memory and do other cleanup for a class objectand its class members when the object is destroyed. A destructor is called for a class object

    when that object passes out of scope or is explicitly deleted.

    A destructor is a member function with the same name as its class prefixed by a ~ (tilde).

    For example:

    class X {

    public:

    // Constructor for class X

    X();

    // Destructor for class X~X();

    };

    A destructor takes no arguments and has no return type. Its address cannot be taken.

    Destructors cannot be declared const, volatile, const volatile orstatic. A

    destructor can be declared virtual or pure virtual.

    If no user-defined destructor exists for a class and one is needed, the compiler implicitly

    declares a destructor. This implicitly declared destructor is an inline public member of itsclass.The compiler will implicitly define an implicitly declared destructor when the

    compiler uses the destructor to destroy an object of the destructor's class type. Thefollowing is equivalent to the function the compiler would implicitly define forA:

    A::~A() { }

    The compiler first implicitly defines the implicitly declared destructors of the base classes

    and nonstatic data members of a class A before defining the implicitly declared destructor

    1. Explain destructors withan example.

    2. Define destructor.Explain the characteristicsof it with examples

    19

  • 7/31/2019 Newformat for SPC UNIT 2

    20/21

    PROGRAM:1

    ofAA destructor of a class A is trivial if all the following are true:

    It is implicitly defined

    All the direct base classes ofA have trivial destructors

    The classes of all the nonstatic data members ofA have trivial destructors

    The destructor for a class object is called before destructors for members and bases arecalled.

    1. Destructors for nonstatic members are called before destructors for base classes arecalled.

    2. Destructors for nonvirtual base classes are called before destructors for virtual base

    classes are called.

    #include

    class Y {

    private:

    char * string;

    int number;

    public:

    // Constructor

    Y(const char*, int);

    // Destructor

    ~Y() { delete[] string; }

    };

    // Define class Y constructor

    Y::Y(const char* n, int a) {string = strcpy(new char[strlen(n) + 1 ], n);

    number = a;

    }

    int main () {

    // Create and initialize

    // object of class Y

    Y yobj = Y("somestring", 10);

    20

  • 7/31/2019 Newformat for SPC UNIT 2

    21/21

    PROGRAM:2

    // ...

    // Destructor ~Y is called before

    // control returns from main()

    }

    We can use a destructor explicitly to destroy objects, although this practice is notrecommended. However to destroy an object created with the placement new operator, We

    can explicitly call the object's destructor. The following example demonstrates this:

    #include

    #include

    using namespace std;

    class A {

    public:

    A() { cout