Introduction to Programming in C - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Introduction to Programming in C

Description:

Imagine that both these base classes have a public member function with the same ... if a class B is derived from class A we can say that 'B is a kind of A' ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 15
Provided by: rohitval
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming in C


1
Introduction to Programming in C
  • Class 22
  • 04/17/2003
  • Rohit Valsakumar

2
Multiple Inheritance
  • A class can be derived from more than one class
  • This is called as multiple inheritance
  • The syntax is as given below
  • class A // Base class A
  • ..
  • class B // Base class B
  • class C public A, public B // C is derived from
    A and B

3
Constructors in Multiple Inheritance
  • No argument constructor for the derived class
  • CC() AA(), BB()
  • Class C is derived from both class A and class B,
    so when we define the no argument constructor for
    class C we have to invoke the no argument
    constructors (if provided) for both base classes
    as shown above
  • These base constructors will be called in order
    when C() constructor is invoked

4
Constructors in Multiple Inheritance
  • Multi-Argument Inheritance
  • Assume a constructor for class A is as given
    below
  • A(int arg1, float arg2)
  • Assume a constructor for class B is as given
    below
  • B(char arg3, double arg4)
  • The constructor for C calls both these
    constructors, so it must supply values for their
    arguments. In addition it has two arguments of
    its own
  • So the constructor for class C has six arguments
    in all
  • CC(int arg1, float arg2,
  • char arg3, double arg4,
  • string arg5, int arg6)
  • AA(arg1, arg2), BB(arg3, arg4)

5
Ambiguity in Multiple Inheritance
  • A class is derived from two base classes
  • Imagine that both these base classes have a
    public member function with the same name
  • The derived class does not override this function
  • Now if an object of the derived class wants to
    access this member function of the base class
    using the dot operator then how will the compiler
    determine which function to call

6
Ambiguity in Multiple Inheritance
  • class A // Base class A
  • public
  • void func1()
  • class B // Base class B
  • public
  • void func1()
  • class C public A, public B // C is derived from
    A and B
  • ...

7
Ambiguity in Multiple Inheritance
  • void main()
  • C objc // object of class C
  • objc.func1() // ambiguous will not
  • // compile
  • objc.Afunc1() // ok
  • objc.Bfunc1() // ok

8
Ambiguity in Multiple Inheritance
  • The problem is resolved using the scope
    resolution operator as shown above
  • objc.Afunc1()
  • Refers to the func1() defined in base class A
  • objc.Bfunc1()
  • Refers to the func1() defined in base class B

9
Containership Classes Within Classes
  • In inheritance if a class B is derived from class
    A we can say that B is a kind of A
  • B has all the characteristics of A and in
    addition some of its own
  • For this reason inheritance is called as a kind
    of relationship
  • There is another kind of relationship, called a
    has a relationship, or containership
  • It occurs when one object is contained in another
  • E.g.,
  • Class A
  • ..
  • B b
  • ..
  • Class B

10
Containership Classes Within Classes
  • Containership is useful with classes that act
    like a data type
  • In that case an object of that type can be used
    in a class in almost the same way a variable
    would be
  • Mostly the inheritance relationship is much
    simpler to implement and offers a cleaner
    conceptual framework

11
Pointers
  • Every byte in the computers memory has an
    address
  • Addresses are numbers
  • The numbers start at 0 and go up from there 1, 2,
    3, ..
  • When a program is loaded into memory it occupies
    a certain range of these addresses
  • Every variable and function in the program starts
    at a particular address

12
The Address Of Operator
  • A variable may occupy several contiguous bytes in
    memory
  • A char occupies 1 byte, an int occupies 4 bytes,
    a double occupies 8 bytes and so on
  • The starting address of each variable is called
    as the address of that variable
  • We can find the starting address of any variable
    using the address of operator
  • The following example demonstrates how to do that

13
Example
  • void main()
  • int var1 11
  • int var2 22 // define and initialize
  • int var3 33 // variables
  • cout ltlt endl ltlt var1 // print out the
  • ltlt endl ltlt var2 // addresses
  • ltlt endl ltlt var3 // of these variables
  • Output

  • 0x8f4ffff4
  • 0x8f4ffff2
  • 0x8f4ffff0

14
The Address Of Operator
  • The address will differ from machine to machine
  • Since the ltlt operator interprets the addresses in
    hexadecimal format we see a 0x before each number
  • The addresses appear in descending order because
    they are stored on the stack which grows downward
    in memory
  • Dont confuse the address of operator , which
    precedes a variable with the reference operator
    , which follows the variable name
Write a Comment
User Comments (0)
About PowerShow.com