C Programming

COMPOSITION AND INHERITANCE

Posted on Updated on

  • In real-life, complex objects are often built from smaller, simpler objects. For example, a car is built using a metal frame, an engine, some tires, a transmission, a steering wheel, and a large number of other parts.
  • A PC is built from a CPU, a motherboard, some memory, etc. This process of building complex objects from simpler ones is called composition (also known as object composition).
  • Composition is used for objects that have a has-a relationship to each other.
  • A car has-a metal frame, has-an engine, and has-a
  • A personal computer has-a CPU, a motherboard, and other components.
  • Composition is nothing but relationships between the different objects. Sometimes Object made up from another objects like Airplane is Wings, Landing gears, engines etc. this relationship is called composition
  • Inheritance is extends one class to another class like

Public class A

{//Here methods and variable etc.}

public class B:A

{//here methods and properties variable etc}

public static void main()

{B b = new B();

b.method();

b.Variable ..}

 INHERITANCE

  • One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.
  • This also provides an opportunity to reuse the code functionality and fast implementation time.
  • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived
  • The idea of inheritance implements the is a For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well

            Base & Derived Classes:

  • A class can be derived from more than one class, which means it can inherit data and functions from multiple base classes.
  • To define a derived class, we use a class derivation list to specify the base class. A class derivation list names one or more base classes and has the form:

class derived-class: access-specifier base-class

  • Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
  • Consider a base class Shape and its derived class Rectangle as follows:

#include <iostream.h>
// Base class
class Shape
{ public:
voidsetWidth(int w)
{ width = w; }
voidsetHeight(int h)
{ height = h; }
protected:
int width;
int height;};
// Derived class
class Rectangle: public Shape
{ public:
intgetArea()
{ return (width * height); }};
void main()
{ Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout<< “Total area: ” <<Rect.getArea() <<endl;}

      When the above code is compiled and executed, it produces the following result:

 Total area: 35

 Total paint cost: $2450

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Advertisements

Reverse the string using c

Posted on

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

 

void main()

{

clrscr();

char a[3][50];

int i,j,k,len;

cout<<“Enter 3 strings:\n”;

 

for(i=0;i<3;i++)

{

gets(a[i]);

}

cout<<“\nThe list of orignal strings:\n” ;

 

for(i=0;i<3;i++)

{

cout<<a[i]<<“\n”;

}

cout<<“\nThe list of changed string:\n”;

 

for(i=0;i<3;i++)

{

len=strlen(a[i]);

for(j=0,k=len-1;k>=0;j++,k–)

{

cout<<a[i][k];

}

cout<<“\n”;

}

getch();

}

5

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com