Windows Media Foundation Architecture


Above diagram shows a high-level view of the Media Foundation architecture.

Media Foundation provides two distinct programming models. The first model, shown on the left side of the diagram, uses an end-to-end pipeline for media data. The application initializes the pipeline—for example, by providing the URL of a file to play—and then calls methods to control streaming. In the second model, shown on the right side of the diagram, the application either pulls data from a source, or pushes it to a destination (or both). This model is particularly useful if you need to process the data, because the application has direct access to the data stream.


Primitives and Platform
Starting from the bottom of the diagram, the primitives are helper objects used throughout the Media Foundation API:

  • Attributes are a generic way to store information inside an object, as a list of key/value pairs.
  • Media Types describe the format of a media data stream.
  • Media Buffers hold chunks of media data, such as video frames and audio samples, and are used to transport data between objects.
  • Media Samples are containers for media buffers. They also contain metadata about the buffers, such as time stamps.
The Media Foundation Platform APIs provide some core functionality that is used by the Media Foundation pipeline, such as asynchronous callbacks and work queues. Certain applications might need to call these APIs directly; also, you will need them if you implement a custom source, transform, or sink for Media Foundation.

Media Pipeline

The media pipeline contains three types of object that generate or process media data:
  • Media Sources introduce data into the pipeline. A media source might get data from a local file, such as a video file; from a network stream; or from a hardware capture device.
  • Media Foundation Transforms (MFTs) process data from a stream. Encoders and decoders are implemented as MFTs.
  • Media Sinks consume the data; for example, by showing video on the display, playing audio, or writing the data to a media file.
Third parties can implement their own custom sources, sinks, and MFTs; for example, to support new media file formats.

The Media Session controls the flow of data through the pipeline, and handles tasks such as quality control, audio/video synchronization, and responding to format changes.

Source Reader and Sink Writer

The Source Reader and Sink Writer provide an alternative way to use the basic Media Foundation components (media sources, transforms, and media sinks). The source reader hosts a media source and zero or more decoders, while the sink writer hosts a media sink and zero or more encoders. You can use the source reader to get compressed or uncompressed data from a media source, and use the sink writer to encode data and send the data to a media sink.

Windows 8 App Platform Architecture


Using SMS in your Application

Android offers full access to SMS functionality from within your applications with the SMSManager. Using the SMS Manager, you can replace the native SMS application or create new applications that send text messages, react to incoming texts, or use SMS as a data transport layer. 

Sending SMS Messages:

SMS messaging in Android is handled by the SmsManager. You can get a reference to the SMS Manager using the static method SmsManger.getDefault, as shown in the snippet below.

SmsManager smsManager = SmsManager.getDefault(); 


To send SMS messages, your applications require the SEND_SMS permission. To request this permission, add it to the manifest using a uses-permission tag, as shown below: 

<uses-permission android:name=”android.permission.SEND_SMS”/>

Sending Text Messages:

To send a text message, use sendTextMessage from the SMS Manager, passing in the address (phone number) of your recipient and the text message you want to send, as shown in the snippet below: 

String sendTo = “5551234”;
String myMessage = “Sample SMS Text!”; 
smsManager.sendTextMessage(sendTo, null, myMessage, null, null); 

The second parameter can be used to specify the SMS service center to use; entering null as shown in the previous snippet uses the default service center for your carrier. 

The final two parameters let you specify Intents to track the transmission and successful delivery of your messages. 


To react to these Intents, create and register Broadcast Receivers. 

Tracking and Confirming SMS Message Delivery:
To track the transmission and delivery success of your outgoing SMS messages, implement and register Broadcast Receivers that listen for the actions you specify when creating the Pending Intents you pass in to the sendTextMessage method. 

The first Pending Intent parameter, sentIntent, is fired when the message is either successfully sent or fails to send. The result code for the Broadcast Receiver that receives this Intent will be one of: 

❑ Activity.RESULT_OK to indicate a successful transmission. 
❑ SmsManager.RESULT_ERROR_GENERIC_FAILURE To indicate a nonspecific failure. 
❑ SmsManager.RESULT_ERROR_RADIO_OFF When the connection radio is turned off. 
❑ SmsManager.RESULT_ERROR_NULL_PDU To indicate a PDU failure. 


Listening for SMS Messages:

When a new SMS message is received by the device, a new broadcast Intent is fired with the android.provider.Telephony.SMS_RECEIVED action.
For an application to listen for SMS Intent broadcasts, it first needs to be have the RECEIVE_SMS permission granted. Request this permission by adding a uses-permission tag to the application manifest, as shown in the following snippet: 

<uses-permission android:name=”android.permission.RECEIVE_SMS”/> 


The SMS broadcast Intent includes the incoming SMS details. To extract the array of SmsMessage objects packaged within the SMS broadcast Intent bundle, use the pdu key to extract an array of SMS pdus, each of which represents an SMS message. To convert each pdu byte array into an SMS Message object, call SmsMessage.createFromPdu, passing in each byte array as shown in the snippet below: 

Bundle bundle = intent.getExtras(); 
if (bundle != null) 

          Object[] pdus = (Object[]) bundle.get(“pdus”); 
          SmsMessage[] messages = new SmsMessage[pdus.length]; 
          for (int i = 0; i < pdus.length; i++) 
                  messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 


Each SmsMessage object contains the SMS message details, including the originating address (phone number), time stamp, and the message body. 

To listen for incoming messages, register the Broadcast Receiver using an Intent Filter that listens for the android.provider.Telephony.SMS_RECEIVED

C/C++ Quiz

Android Architecture


What is Android?

  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications.
  • Android is a mobile operating system running on the Linux kernel. It was initially developed by Android Inc., a firm later purchased by Google, and later by the Open Handset Alliance.
  • It allows developers to write managed code in the Java language, controlling the device via Google-developed Java libraries.
  • The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.

Purpose of main() function in C?

In C, program execution starts from the main() function. Every C program must contain a main() function. The main function may contain any number of statements. These statements are executed sequentially in the order which they are written.

The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.

In C, the function prototype of the 'main' is one of the following:
int main(); //main with no arguments
int main(int argc, char *argv[]); //main with arguments

The parameters argc and argv respectively give the number and value of the program's command-line arguments.

Example:

#include <stdio.h>
/* program section begins here */
int main()
{
          // opening brace - program execution starts here
          printf("Welcome to the world of C");
          return 0;
}
// closing brace - program terminates here

Output:
Welcome to the world of C

Which bitwise operator is suitable for checking whether a particular bit is ON or OFF?

Bitwise AND operator.

Example: Suppose in byte that has a value 10101101 . We wish to check whether bit number 3 is ON (1) or OFF(0) . Since we want to check the bit number 3, the second operand for AND operation we choose is binary 00001000, which is equal to 8 in decimal.

Explanation:

ANDing operation :

10101101 original bit pattern
00001000 AND mask
--------------
00001000 resulting bit pattern
--------------

The resulting value we get in this case is 8, i.e. the value of the second operand. The result turned out to be a 8 since the third bit of operand was ON. Had it been OFF, the bit number 3 in the resulting bit pattern would have evaluated to 0 and complete bit pattern would have been 00000000. Thus depending upon the bit number to be checked in the first operand we decide the second operand, and on ANDing these two operands the result decides whether the bit was ON or OFF.

What is pass by reference in functions?

Pass by Reference: In this method, the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses, we would have an access to the actual arguments and hence we would be able to manipulate them. C does not support Call by reference.

But it can be simulated using pointers.

Example:

#include <stdio.h>
/* function definition */
void swap(int *x, int *y) 
{
        int t;
        t = *x; /* assign the value at address x to t */
       *x = *y; /* put the value at y into x */
       *y = t; /* put the value at to y */
int main() 
{
        int m = 10, n = 20;        
        printf("Before executing swap m=%d n=%d\n", m, n);
        swap(&m, &n);
        printf("After executing swap m=%d n=%d\n", m, n);
     return 0;
}


Output:
Before executing swap m=10 n=20
After executing swap m=20 n=10

Explanation:
In the main function, address of variables m, n are sent as arguments to the function 'swap'. As swap function has the access to address of the arguments, manipulation of passed arguments inside swap function would be directly reflected in the values of m, n.

Write a program that returns 3 numbers from a function using a structure?

A function in C can return only one value. If we want the function to return multiple values, we need to create a structure variable, which has three integer members and return this structure.

Program: Program with a function to return 3 values

#include<stdio.h>
//sample structure which has three integer variables.
struct sample 
{    
         int a, b, c;
};
//this is function which returns three values.
struct sample return3val() 
{
         struct sample s1;
         s1.a = 10;
         s1.b = 20;
         s1.c = 30;
        //return structure s1, which means return s1.a ,s1.b and s1.c
        return s1;
}
int main() 
{
        struct sample accept3val;
        //three values returned are accepted by structure accept3val.
        accept3val = return3val();
        //prints the values
        printf(" \n %d", accept3val.a);
        printf("\n %d", accept3val.b);
        printf(" \n %d", accept3val.c);
       return 0;
}

Output:
10
20
30.

Explanation:

In this program, we use C structure to return multiple values from a function. Here we have a structure holding three int variables and a function which returns it. 'return3val' is a function which assigns 10, 20, 30 to its integer variables and returns this structure. In this program, 'accept3val' is a structure used to accept the values returned by the function. It accepts those values and shows the output.

What is the difference between declaring a variable and defining a variable?

Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined.

What are advantages and disadvantages of external storage class?

Advantages of external storage class
1)Persistent storage of a variable retains the latest value
2)The value is globally available


Disadvantages of external storage class

1)The storage for an external variable exists even when the variable is not needed
2)The side effect may produce surprising output
3)Modification of the program is difficult
4)Generality of a program is affected

Differentiate between an internal static and external static variable?

An internal static variable is declared inside a block with static storage class whereas an external static variable is declared outside all the blocks in a file.An internal static variable has persistent storage,block scope and no linkage.An external static variable has permanentstorage,file scope and internal linkage.

Android Multimedia

  1. Android Multimedia Introduction
  2. Player States
  3. Stagefright Framework
  4. Audio Playback
  5. Video Playback
  6. Open Max
  7. Codecs

Can static variables be declared in a header file?

You can’t declare a static variable without defining it as well (this is because the storage class modifiers static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended.

C/C++ Interview Questions

1)What is the output of printf(”%d”) 
2) What will happen if I say delete this
3) Difference between “C structure” and “C++ structure”.
4) Diffrence between a “assignment operator” and a “copy constructor”
5) What is the difference between “overloading” and “overridding”?
6) Explain the need for “Virtual Destructor”.
7) Can we have “Virtual Constructors”?
8)What are the different types of polymorphism?
9)What are Virtual Functions? How to implement virtual functions in “C”
10)What are the different types of Storage classes?
11)What is Namespace?
12)What are the types of STL containers?.
13)Difference between “vector” and “array”?
14)How to write a program such that it will delete itself after exectution?
15)Can we generate a C++ source code from the binary file?
16)What are inline functions?
17)What is “strstream” ?
18)Explain “passing by value”, “passing by pointer” and “passing by reference”
19)Have you heard of “mutable” keyword?
20)What is a “RTTI”?
21)Is there something that I can do in C and not in C++?
22)What is the difference between “calloc” and “malloc”?
23)What will happen if I allocate memory using “new” and free it using “free” or allocate sing “calloc” and free it using “delete”?
24)Difference between “printf” and “sprintf”.
25)What is “map” in STL?
26)When shall I use Multiple Inheritance?
27)Explain working of printf?
28)Talk sometiming about profiling?
29)How many lines of code you have written for a single program?
30)How to write Multithreaded applications using C++?
31)Write any small program that will compile in “C” but not in “C++”
32)What is Memory Alignment?
33)Why preincrement operator is faster than postincrement?
34)What are the techniques you use for debugging?
35)How to reduce a final size of executable?
36)Give 2 examples of a code optimization.

Inheritance

Inheritance:The C++ classes can be reused in several ways. Once a class has been written and tested, it can be adapted by other programmers to suit their requirements. This is basically done by creating new classes, reusing the properties of the existing ones. The mechanism of deriving a new class from an old one is called inheritance ( or derivation ). The old class is referred to as the base class (superclass) and the new one is called the derived class (subclass). Inheritance is not only used for re-using the classes, but as major aid for structuring the complex problems, breaking them into meaningful modular objects representing the problem to be solved. 

Different forms of Inheritance:
The derived class inherits some or all of the traits from the base class. A class can also inherit properties from more than one or from more than one level. A derived class with only one base class is called single inheritance and one with several base classes is called multiple inheritance. 
The traits of one class may be inherited by more than one class. This process is known as hierarchical inheritance. The mechanism of deriving a class from another derived class is known as multilevel inheritance. 
The following figure shows the different forms of inheritance. The direction of arrow indicates the direction of inheritance. 



The most important benefit inheritance brings is the ability to classify a complex problem or knowledge base into hierarchical classification. For example Alto is part of the classification of Maruti Cars or Compact Cars or both (Multiple inheritance), which is under larger classifications of cars, which in turn is under the more general classification of Automobiles and so on. Without this concept each object would have to be defined explicitly. With the help of inheritance we can simple add those qualities that make it unique from the generic common form. 

Defining Derived Classes:
A derived class can be defined by specifying its relationship with the base class in addition to its own details. The general form of defining a derived class is: 

class derived-class-name : visibility-mode base-class-name
{
        ...............//
        ...............// members of derived class
        ...............//
}The colon indicates that the derived-class-name is derived from the base-class-name. The visibility-mode or access control is optional and if present, may be private ,public or protected. The default visibility mode is private.
Examples: 
class ABC: private XYZ                    //private derivation
{
           .............
           .............
};
class ABC: public XYZ                     //public derivation
{
           .............
           .............
};
class ABC: XYZ                                //private derivation by default
{
          ..............
          ..............
};

Typically the derived object has all the information that a base class has. In addition all the specialized information for the derived object is added at the end of the base class.

Notes:
Derived class can access the public and protected members of the base class.
Private members of the base class cannot be accessed directly.

For eg:  if Manager is derived from Employee then the representation can be as follows:


For the above example if we have emp and mgr as obejects of Employee and Manager class respectively and we have the respective pointers pemp* and pmgr*, it is always possible to use pemp* to represent both the base and derived class. 
                    pemp = &emp; //Valid;                     pemp = &mgr; //Still Valid 
pemp->Name, pemp->EMp_ID and pemp->Date_of_joining are also valid for the derived Manager object. Use of pemp* to represent Manager object has no side effects. Other way round, representing an Employee (base class object) with Manager* (derived object pointer) is not valid as pmgr->Date_of_Promotion_to_Mgr and pmgr->list_of_reportees are not valid for Employee object. 

Types of Inheritance:
C++ distinguishes three types of inheritance: public, private and protected. As a default, classes are privately derived from each other. Consequently, we must explicitly tell the compiler to use public inheritance. 

The type of inheritance influences the access rights to elements of the various superclasses. Using public inheritance, everything which is declared private in a superclass remains private in the subclass. Similarly, everything which is public remains public. The following table specifies the type of access of the base class members in the derived class.


The leftmost column lists possible access rights for elements of classes. The second to fourth column show the resulting access right of the elements of a superclass when the subclass is derived using the access control specified mentioned in the top row.
Relationship 
“Is-a relationship” 
Public Inheritance should be used when you want to have the subset of properties of the base class to the new class. This kind of relationship is called the “Is-a relationship”. (Generalization – Specialization). 
class Employee 

         private: 
                     std::string Name; 
                     int EmpID; 
                     std::string Date_of_Joining; 
}; 
class Manager : public Employee 

         private: 
                     std::string Date_of_Promotion_to_Mgr; 
                         ….. 
         public: 
                     // public members 
}; 
Here, Manager is an Employee. Every Manager is an Employee of the company. Every Manager object will have the properties of Employee objects too. Every operation that can be applied to Employee should also make sense when applied to Manager Objects. This is called “Is-a Relationship”. 

“Has-a relationship”
Public Inheritance should not be used when the base class is a component of the object described by the derived class. 

class Wheel 

         public: 
                    int size() const; 
}; 
class Car : public Wheel 

        private: 
                 // Data Member and functions. 
        public: 
                // Public Member data and functions 
}; 
A Car is not a specialization of Wheel class. But, a car has a Wheel. This inappropriate use of public inheritance allows users to apply Wheel operations on Car. 

Car mycar;
int cap = mycar.size(); // This is not what we want. This will return the size of wheel, not the size of car. 

There are two ways to represent this “Has-a relationship”

We can have a composition. 
class Car 

          private: 
                      Wheel W; 
          Public: 
                      // etc., 
}; 

Or we can also use private inheritance to achieve this. 
class Car : private Wheel 

          private: 
                 // Private Members 
         public: 
                // Public Members 
}; 
Private inheritance is not much used than public inheritance, because composition is simpler and usually works as well. There are some advantages / disadvantages using private / protected inheritance than composition / containment. 
Advantages: 
The derived class can override private base class’s virtual functions 
The derived class can access to the protected members of the base class
Disadvantages: 
The private / protected inheritance can introduce unnecessary multiple inheritance if you want your class to be publicly derived from other class.
Composition is simpler than private / protected inheritance in terms of access and code maintainability.


Constructors and Destructors in Derived Classes:
The important thing to note is, when both the derived and base classes contain constructors, the base constructor is executed first and then the constructor in the derived class is executed. 

Similarly when the object is destroyed, derived class destructor will be called first and then the base class destructor.

In case of multiple inheritance, the base classes are constructed in the order in which they appear in the declaration of the derived class. Similarly, in a multilevel inheritance, the constructors will be executed in the order of inheritance.
Passing parameters to the base class constructor:
Initialization list is the method of initializing the class objects in the constructor. The following is the syntax of initialization list. 
constructor (arglist) : initialization-section 

            assignment-section 

The assignment-section is nothing but the body of the constructor function and is used to assign initial values to its data member. 

The initialization section basically contains a list of initializations separated by commas. This section is used to provide initial values to the base constructors and also to initialize its own class members. Thus we can use either of the sections to initialize the data members of the constructor class.

The following program illustrates the use of initialization lists in the base and derived constructors. Also this demonstrates the execution order of inheritance.
// Initialization List in the constructors 
// Constructors and destructors called. 
#include <iostream> 
using namespace std; 
class Alpha 

        int x; 
        public: 
                // constructors 
                Alpha(int i) 
                { 
                        x = i; 
                        cout << "\n Alpha constructed"; 
                } 
               ~Alpha() 
                { 
                       cout << "\n Alpha destroyed"; 
                } 
               void show_alpha() 
               { 
                      cout << " X = " << x << "\n"; 
               } 
}; 
class Beta : public Alpha 

       float p, q; 
       public: 
                  // Constructors 
                Beta(int i, float a, float b):Alpha(i), p(a), q(b+p) 
                { 
                       cout << "\n Beta Constructed"; 
                } 
                ~Beta() 
                 { 
                       cout << "\n Beta destroyed"; 
                 } 
                void show_beta() 
                { 
                       cout << " P = " << p << "\n"; 
                       cout << " Q = " << q << "\n"; 
                } 
}; 
class delta 

       int d; 
       public: 
                 delta(int a) { d = a; cout << "\n Delta Constructed";} 
                 ~delta() { cout << "\n Delta destroyed"; } 
                 void show_delta() 
                 { 
                         cout << " d = " << d << "\n"; 
                 } 
}; 
class Gamma : public Beta, public delta 

        int u, v; 
        public: 
                  // Constructors 
                  Gamma(int a, int b, float c): Beta(a,c,c), delta(a), u(a) 
                   { 
                            v = b; 
                           cout << "\n Gamma constructed"; 
                   } 
                   ~Gamma() 
                   { 
                            cout << "\n Gamma destroyed"; 
                   } 
                  void show_gamma() 
                  { 
                           cout << " U = " << u << "\n"; 
                           cout << " V = " << v << "\n"; 
                  } 
}; 
int main() 

        Gamma g(2, 4, 2.5); 
        cout << "\n\n Display member values " << "\n\n"; 
        g.show_alpha(); 
        g.show_beta(); 
        g.show_delta(); 
        g.show_gamma(); 
   return 0; 

Above program produces following output: 
Alpha constructed 
Beta Constructed 
Delta Constructed 
Gamma constructed 

Display member values
X = 2 
P = 2.5 
Q = 5 
d = 2 
U = 2 
V = 4 
Gamma destroyed 
Delta destroyed 
Beta destroyed 
Alpha destroyed 


Replicated Classes and Virtual Base Class:

A modified version of the sample code that we had seen in the last week to understand the arguments passing to the base class constructor and order of execution of the constructor and destructor of the base & derived class is listed below:
#include <iostream> 
using namespace std; 
class Alpha 

           int x; 
           public:
                    // constructors 
                    Alpha(int i) 
                    { 
                           x = i; 
                           cout << "\n Alpha constructed"; 
                    } 
                   ~Alpha() 
                    { 
                           cout << "\n Alpha destroyed"; 
                    } 
                   void show_alpha() 
                   { 
                          cout << " X = " << x << "\n"; 
                   } 
}; 
class Beta : public Alpha 
{                                  
          float p, q;           public: 
                   // Constructors 
                   Beta(int i, float a, float b):Alpha(i), p(a), q(b+p) 
                   { 
                                 cout << "\n Beta Constructed"; 
                   } 
                   ~Beta() 
                   { 
                                 cout << "\n Beta destroyed"; 
                   } 
                  void show_beta() 
                  { 
                                  cout << " P = " << p << "\n"; 
                                  cout << " Q = " << q << "\n"; 
                  } 
}; 
class delta : public Alpha 

          int d; 
          public: 
                     delta(int i, int a): Alpha (i) { d = a; cout << "\n Delta Constructed";} 
                      ~delta() { cout << "\n Delta destroyed"; } 
                      void show_delta() 
                      { 
                                  cout << " d = " << d << "\n"; 
                      } 
}; 
//Gama is derived using Beta and delta. Both in turn are derived from Alpha. 
//As a result there are two Alpha objects are created for each Gama object. 
class Gamma : public Beta, public delta 

          int u, v; 
          public: 
                    // Constructors
                   Gamma(int a, int b, float c): 
                   Beta(a,c,c), delta(a,b), u(a) 
                   { 
                            v = b; 
                            cout << "\n Gamma constructed"; 
                   } 
                  ~Gamma() 
                   { 
                            cout << "\n Gamma destroyed"; 
                   } 
                  void show_gamma() 
                  { 
                           cout << " U = " << u << "\n"; 
                           cout << " V = " << v << "\n"; 
                  } 
}; 
int main() 


         Gamma g(2, 4, 2.5);
         cout << "\n\n Display member values " << "\n\n"; 
         g.show_alpha(); //Ambiguous For which Alpha object? 
         g.show_beta(); 
         g.show_delta(); 
         g.show_gamma(); 
       return 0; 


The above example shows that ambiguity may be introduced when multiple base classes are inherited that in turn inherit from a common base class.

One way to remove this ambiguity is to manually resolve it by using the scope resolution operator. You may change the ambiguous statement as:

g.Beta::show_alpha() 

// Now it is clear that it has use the copy of Alpha that is
// created as part of Beta. 

This is not the perfect solution as in most of the cases intention is not to create multiple copies of the base class any way. C++ provides the concept of virtual base class to address this issue. If you declare a base class as virtual while inheriting only one copy of the base class object is created even if it is derived through multiple classes. The above example is modified using the virtual base class to have only single copy of Alpha in Gama:
#include <iostream> 
using namespace std; 
class Alpha 

        int x; 
        public: 
                 // constructors 
                Alpha(int i=0) 
                { 
                          x = i; 
                          cout << "\n Alpha constructed"; 
                } 
                ~Alpha()
                { 
                          cout << "\n Alpha destroyed"; 
                } 
                void show_alpha() 
                { 
                          cout << " X = " << x << "\n"; 
                } 
}; 
class Beta : virtual public Alpha 

           float p, q; 
           public: 
                      // Constructors 
                      Beta(int i, float a, float b): p(a), q(b+p) 
                      { 
                               cout << "\n Beta Constructed"; 
                      } 
                      ~Beta() 
                      { 
                               cout << "\n Beta destroyed"; 
                      } 
                      void show_beta() 
                       { 
                               cout << " P = " << p << "\n"; 
                               cout << " Q = " << q << "\n"; 
                        } 
}; 
class delta : virtual public Alpha 

          int d; 
          public: 
                     delta(int i, int a) { d = a; cout << "\n Delta Constructed";} 
                     ~delta() { cout << "\n Delta destroyed"; } 
                      void show_delta() 
                      { 
                               cout << " d = " << d << "\n"; 
                      } 
}; 
// Gama is derived using Beta and delta. Both in turn are derived from Alpha. 
// Since both have derived Alpha as virtual base, there will be only one 
// copy of Alpha in Gama. 

class Gamma : virtual public Beta, virtual public delta

         int u, v; 
         public: 
                   // Constructors, Explicitly calling the constructed of an 
                   // indirect virtual base class. 
                  Gamma(int a, int b, float c): Alpha(a), delta(a,b), Beta(a,c,c), u(a) 
                  { 
                           v = b; 
                          cout << "\n Gamma constructed"; 
                  } 
                  ~Gamma() 
                  { 
                          cout << "\n Gamma destroyed"; 
                  } 
                 void show_gamma() 
                 { 
                          cout << " U = " << u << "\n"; 
                          cout << " V = " << v << "\n"; 
                 } 
}; 
int main() 

         Gamma g(2, 4, 2.5); 
         cout << "\n\n Display member values " << "\n\n"; 
         g.show_alpha(); 
         g.show_beta(); 
         g.show_delta(); 
         g.show_gamma(); 
    return 0; 

Following is the output produced by the above program: 
Alpha constructed 
Beta Constructed 
Delta Constructed 
Gamma constructed 
Display member values 
X = 2 
P = 2.5 
Q = 5 
d = 4 
U = 2 
V = 4 
Gamma destroyed 
Delta destroyed 
Beta destroyed 

Please note that even though both Beta and delta have declared Alpha as virtual, a copy of Alpha is still present in objects their type.
Please also note that Alpha’s constructor is explicitly called from Gama to initialize the value of x. If it not called explicitly then the default constructor will be called and the value of the x would be 0. 

Important points:
  • Arguments for the base class’ constructor have to be defined in the definition of a derived class’ constructor. 
  • A derived class constructor can only specify initializer for it’s own members and immediate base class only. It can’t initialize members of a base. 
  • For example in the above case Gama constructor can’t pass the argument for Alpha constructor directly. It has to do it through Beta. Secondly it can’t initialize members of base directly like 
                       Gama (int a, int b, float c) : Beta (a,c,c), d(a), u(a) 

                       //Invalid, d is member of delta base class.
  • Private inheritance (has-a relationship) can be converted into containment. Whenever possible use containment and if required use private inheritance. 
  • Use Multiple Inheritance judiciously. 
  • Multiple inheritances produce duplicate objects. This can be resolved using virtual public inheritance.