Logo

Visual C++ Keywords: __interface

 

Introduction

Inheritance is the ability to use one class to provide the basic or primary behavior for another class. Inheritance is very important in C++ and all of the (commercial and non-commercial) libraries rely on it for code efficiency. This is why this concept is highly enforced in Managed C++.

Managed C++ supports inheritance at various levels, one of which is through the __abstract keyword.:

 

The Influence of Interfacing

In order to use inheritance, one class considered the parent must have been created and made available to other classes. As done with abstraction, sometimes you may want to create a class that would be used only to provide a foundation for other classes. Such a class would never be instantiated. Such a class is called an interface.

To create an interface class, you can use the __interface keyword. It is important to note that this __interface concept doesn't add any genuine functionality to the Managed C++ language. It only enforces the notion of abstraction.

When creating an interface class, type the __interface keyword on the left of the name of the class. An interface is created like a class, except that it uses the __interface keyword. By (good) habit, it is usual to start the name of an interface class with I to indicate the nature of the class. Here is an example that creates an interface class:

Header File: quadrilateral.h
#pragma once
__interface IQuadrilateral
{
	double Perimeter();
	double Area();
};
 

As mentioned already, the keyword __interface indicates that the class is abstract. Therefore, you don't have to specify that its methods are virtual. However, if you want, you can still type the virtual keyword to the left of the method.

__interface IQuadrilateral
{
	double Perimeter();
	virtual double Area();
};

Also, since an interface is only made of pure virtual method, you don't have to indicate this by typing = 0; to their left, but you can if you want to:

__interface IQuadrilateral
{
	double Perimeter();
	virtual double Area() = 0;
};

In case you wonder why we declared only methods in the class, an interface class can have only methods, no member variables. Here are other rules to follow:

  • An interface class can inherit from only another interface class, not any regular class
  • An interface class can have only methods, no member variables; the methods are already pure virtual
  • The methods of an interface class can only be public; private or protected methods are not allowed
  • Constructors and destructors are not allowed in an interface class; when you derive a class from it, you can create the necessary constructors and destructor
  • Static methods are not allowed in an interface class

After creating an interface, you can derive a class from it as you see fit, following the rules of abstraction. Here is an example of a class derived from an interface:

Header File: quadrilateral.h
#pragma once

public __gc __interface IQuadrilateral
{
	double Perimeter();
	virtual double Area();
};
Header File: square.h
#pragma once
#include "quadrilateral.h"

public __gc class CSquare :	public IQuadrilateral
{
private:
	double side;
public:
	CSquare(void);
	virtual ~CSquare(void);
public:
	void setSide(double s);
	double getSide();
	virtual double Perimeter();
	virtual double Area();
};
Source File: square.h
#include "StdAfx.h"
#include ".\square.h"
#using <mscorlib.dll>

CSquare::CSquare(void)
	: side(0.00)
{
}

CSquare::~CSquare(void)
{
}

void CSquare::setSide(double s)
{
	side = (s <= 0.00) ? 0.00 : s;
}

double CSquare::getSide()
{
	return side;
}

double CSquare::Perimeter()
{
	return 4 * side;
}

double CSquare::Area()
{
	return side * side;
}

The class can then be used in an application by declaring an instance of it. Here is an example:

 

#pragma once

#include ".\Square.h"

namespace Figures
{
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary> 
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the 
	///          'Resource File Name' property for the managed resource compiler tool 
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public __gc class Form1 : public System::Windows::Forms::Form
	{	
	public:
		Form1(void)
		{
			InitializeComponent();
		}
  
	protected:
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
	private: System::Windows::Forms::GroupBox *  groupBox1;
	private: System::Windows::Forms::Label *  label1;
	private: System::Windows::Forms::Label *  label2;
	private: System::Windows::Forms::Label *  label3;
	private: System::Windows::Forms::TextBox *  txtSide;
	private: System::Windows::Forms::TextBox *  txtPerimeter;
	private: System::Windows::Forms::TextBox *  txtArea;
	private: System::Windows::Forms::Button *  btnCalculate;
	private: System::Windows::Forms::Button *  btnClose;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container * components;

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->groupBox1 = new System::Windows::Forms::GroupBox();
			this->label1 = new System::Windows::Forms::Label();
			this->label2 = new System::Windows::Forms::Label();
			this->label3 = new System::Windows::Forms::Label();
			this->txtSide = new System::Windows::Forms::TextBox();
			this->txtPerimeter = new System::Windows::Forms::TextBox();
			this->txtArea = new System::Windows::Forms::TextBox();
			this->btnCalculate = new System::Windows::Forms::Button();
			this->btnClose = new System::Windows::Forms::Button();
			this->groupBox1->SuspendLayout();
			this->SuspendLayout();
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->btnClose);
			this->groupBox1->Controls->Add(this->btnCalculate);
			this->groupBox1->Controls->Add(this->txtArea);
			this->groupBox1->Controls->Add(this->txtPerimeter);
			this->groupBox1->Controls->Add(this->txtSide);
			this->groupBox1->Controls->Add(this->label3);
			this->groupBox1->Controls->Add(this->label2);
			this->groupBox1->Controls->Add(this->label1);
			this->groupBox1->Location = System::Drawing::Point(16, 16);
			this->groupBox1->Name = S"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(280, 136);
			this->groupBox1->TabIndex = 0;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = S"Square Processing";
			// 
			// label1
			// 
			this->label1->Location = System::Drawing::Point(16, 32);
			this->label1->Name = S"label1";
			this->label1->Size = System::Drawing::Size(80, 23);
			this->label1->TabIndex = 0;
			this->label1->Text = S"Side:";
			// 
			// label2
			// 
			this->label2->Location = System::Drawing::Point(16, 64);
			this->label2->Name = S"label2";
			this->label2->Size = System::Drawing::Size(80, 23);
			this->label2->TabIndex = 1;
			this->label2->Text = S"Perimeter:";
			// 
			// label3
			// 
			this->label3->Location = System::Drawing::Point(16, 96);
			this->label3->Name = S"label3";
			this->label3->Size = System::Drawing::Size(80, 23);
			this->label3->TabIndex = 2;
			this->label3->Text = S"Area:";
			// 
			// txtSide
			// 
			this->txtSide->Location = System::Drawing::Point(96, 32);
			this->txtSide->Name = S"txtSide";
			this->txtSide->Size = System::Drawing::Size(80, 20);
			this->txtSide->TabIndex = 3;
			this->txtSide->Text = S"0.00";
			this->txtSide->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			// 
			// txtPerimeter
			// 
			this->txtPerimeter->Location = System::Drawing::Point(96, 64);
			this->txtPerimeter->Name = S"txtPerimeter";
			this->txtPerimeter->Size = System::Drawing::Size(80, 20);
			this->txtPerimeter->TabIndex = 4;
			this->txtPerimeter->Text = S"0.00";
			this->txtPerimeter->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			// 
			// txtArea
			// 
			this->txtArea->Location = System::Drawing::Point(96, 96);
			this->txtArea->Name = S"txtArea";
			this->txtArea->Size = System::Drawing::Size(80, 20);
			this->txtArea->TabIndex = 5;
			this->txtArea->Text = S"0.00";
			this->txtArea->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			// 
			// btnCalculate
			// 
			this->btnCalculate->Location = System::Drawing::Point(184, 32);
			this->btnCalculate->Name = S"btnCalculate";
			this->btnCalculate->TabIndex = 6;
			this->btnCalculate->Text = S"Calculate";
			this->btnCalculate->Click += new System::EventHandler(this, btnCalculate_Click);
			// 
			// btnClose
			// 
			this->btnClose->Location = System::Drawing::Point(184, 96);
			this->btnClose->Name = S"btnClose";
			this->btnClose->TabIndex = 7;
			this->btnClose->Text = S"Close";
			// 
			// Form1
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
			this->ClientSize = System::Drawing::Size(314, 168);
			this->Controls->Add(this->groupBox1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = S"Form1";
			this->Text = S"Interfaces";
			this->groupBox1->ResumeLayout(false);
			this->ResumeLayout(false);

		}	
	private: System::Void btnCalculate_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				 CSquare __gc *Sqr = __gc new CSquare;

				 double s = this->txtSide->Text->ToDouble(0);
				 Sqr->setSide(s);
				 double Perim = Sqr->Perimeter();
				 double Ar    = Sqr->Area();

				 this->txtPerimeter->Text = Perim.ToString();
				 this->txtArea->Text      = Ar.ToString();
			 }
private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
		 {
			 Close();
		 }
};
}

Related Subject

__abstract

Home Copyright © 2004-2010 FunctionX, Inc.