C++.NET
Windows Forms Course
L11-Inheritance

Mohammad Shaker
mohammadshakergtr.wordpress.com
C++.NET Windows Forms Course
@ZGTRShaker
Inheritance
the concept
Inheritance
• Now, let’s have the following class:
#pragma once
using namespace::System;
ref class MyClass
{
public:
MyClass(void);
virtual String ^ToString() override;
};
Inheritance

#include "StdAfx.h"
#include "MyClass.h"
MyClass::MyClass(void)
{}

String^ MyClass::ToString()
{
return "I won't red 3leek :P:P ";
};
Inheritance
• What happens?
private: System::Void button1_Click(System::Object^
System::EventArgs^ e)
{
MyClass ^MC = gcnew MyClass;
textBox1->Text = MC->ToString();
}

sender,
Inheritance
• Let’s get it bigger a little
#pragma once
using namespace::System;
ref class MyClass
{
public:
MyClass(String^ s1, String^ s2);
virtual String ^ToString() override;
private:
String ^FName;
String ^LName;
};
Inheritance
#include "StdAfx.h"
#include "MyClass.h"
MyClass::MyClass(String^ s1, String^ s2)
{
FName = s1; LName = s2;
}

String^ MyClass::ToString()
{
return String::Format("{0}{1}{2}{3}", "My name is : ",
FName, " ", LName);
};
Inheritance
• What happens?
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
MyClass ^MC = gcnew MyClass("MeMe", "Auf-Wiedersehen");
textBox1->Text = MC->ToString();
}
Inheritance
• Let’s have the following ref class.
• Everything ok?
#pragma once

ref class MyClass : System::Windows::Forms::Button
{
public:
MyClass(void);
};
Compiler error, why?
Inheritance
#pragma once
using namespace System;

ref class MyClass : System::Windows::Forms::Button
{

public:
MyClass(void);
};
Inheritance
• Why doing this?
#pragma once
using namespace System;
namespace dotNet8_Inher {
ref class Form1;

Forward declaration

ref class MyClass : System::Windows::Forms::Button
{
public: MyClass(void);
};
}
Inheritance
• .cpp file
#include "StdAfx.h"
#include "MyClass.h"
namespace dotNet8_Inher {
MyClass::MyClass(void)
{}
}
Inheritance
• In Form1.h
private: System::Void Form1_Load(System::Object^
sender, System::EventArgs^ e)
{
MyClass ^MC = gcnew MyClass;
}

• Sth needed?  for Controls?
• In Form1.h
private: System::Void Form1_Load(System::Object^
sender, System::EventArgs^ e)
{
MyClass ^MC = gcnew MyClass;
MC->Parent = this;
}

• What will happen now?
Inheritance
Inheritance
• Now, let’s have the following in cpp file
#include "StdAfx.h"
#include "MyClass.h"

namespace dotNet8_Inher {
MyClass::MyClass(void)
{
this->Text = "I'M HAPPY!";
}
}
Inheritance
• Again, in Form1.h
private: System::Void Form1_Load(System::Object^
sender, System::EventArgs^ e)
{
MyClass ^MC = gcnew MyClass;
MC ->Parent = this;
}

• What will happen now?
Inheritance
#include "StdAfx.h"
#include "MyClass.h"

namespace dotNet8_Inher {
MyClass::MyClass(void)
{
this->Location = System::Drawing::Point(223, 121);
this->Name = L"button1";
this->Size = System::Drawing::Size(75, 23);
this->TabIndex = 0;
this->Text = L"My Button!";
this->UseVisualStyleBackColor = true;
}
}
private: System::Void Form1_Load(System::Object^
System::EventArgs^ e)
{
MyClass ^MC = gcnew MyClass;
MC ->Parent = this;
}

sender,
Inheritance
A way to steal :D
Multiple Inheritance
The concept
Multiple Inheritance
• Now, let’s see the following crazy code
#pragma once
using namespace System;
namespace dotNet8_Inheir {
ref class Form1;
ref class MyClass : System::Windows::Forms::Button,
System::Windows::Forms::ComboBox
{
private:
Form1 ^MyForm;
public:
MyClass(Form1 ^f);
};
}
#include "StdAfx.h"
#include "MyClass.h"

namespace dotNet8_Inheir {

MyClass::MyClass(void)
{
this->Location = System::Drawing::Point(223, 121);
this->Name = L"button1";
this->Size = System::Drawing::Size(75, 23);
this->TabIndex = 0;
this->Text = L"My Button!";
this->UseVisualStyleBackColor = true;
}
}
private: System::Void Form1_Load(System::Object^
System::EventArgs^ e)
{
MyClass ^MC = gcnew MyClass;
MC ->Parent = this;
}

sender,
Compiler error
Ambiguous, Why?
Multiple Inheritance
• Why?
– Can’t know the “location” peoperties is for!
– Can’t inhert from more than one base class in.NET!

• So, what to do?
– Dump fix. (Do Not Do it (DNDI) unlsess necessary)
Multiple Inheritance - DNDI
#pragma once
using namespace System;
namespace dotNet8_Inheir {
ref class Form1;
ref class MyClass : public System::Windows::Forms::Button
{
private:
Form1 ^MyForm;
System::Windows::Forms::ComboBox ^MyCB;
public:
MyClass(Form1 ^f);
void InitializeButton();
void
InitializeComboBox(System::Windows::Forms::ComboBox ^%);
};
}
#include "StdAfx.h"
#include "MyClass.h”
namespace dotNet8_Inheir
{
MyClass::MyClass(Form1 ^f)
{
MyForm = f;
InitializeButton();
InitializeComboBox(MyCB);
}
void MyClass::InitializeButton()
{
this->Location = System::Drawing::Point(223, 121);
this->Name = L"button1";
this->Size = System::Drawing::Size(75, 23);
this->TabIndex = 0;
this->Text = L"My Button!";
this->UseVisualStyleBackColor = true;
}
void MyClass::InitializeComboBox(System::Windows::Forms::ComboBox ^%CB)
{
CB = gcnew System::Windows::Forms::ComboBox;
CB->Location = System::Drawing::Point(223, 121);
CB->Size = System::Drawing::Size(75, 23);
CB->TabIndex = 0;
CB->Text = L"My ComboBox!";
CB->Parent = this;
}
}
Multiple Inheritance - DNDI
• Let’s start all over again
private: System::Void Form1_Load(System::Object^
System::EventArgs^ e)
{
MyClass ^MC;
MC = gcnew MyClass(this);
}

sender,
Multiple Inheritance - DNDI
#pragma once
using namespace System;
namespace dotNet8_Inheir {
ref class Form1;
ref class MyClass : public System::Windows::Forms::Button
{
private:
Form1 ^MyForm;
System::Windows::Forms::ComboBox ^MyCB;
public:
MyClass(Form1 ^f);
void InitializeButton();
void InitializeComboBox(System::Windows::Forms::ComboBox ^%);
void PlayIt(System::Object^ sender, System::EventArgs^ e);
void comboBox1_SelectedIndexChanged(System::Object^ sender,
System::EventArgs^ e);
};
}
#include "StdAfx.h"
#include "MyClass.h"
#include "Form1.h"
namespace dotNet8_Inheir
{
MyClass::MyClass(Form1 ^f)
{
MyForm = f;
this->Parent = MyForm;
InitializeButton();
InitializeComboBox(MyCB);
}

void MyClass::InitializeButton()
{
this->Button::Location = System::Drawing::Point(223, 121);
this->Name = L"button1";
this->Size = System::Drawing::Size(75, 23);
this->TabIndex = 0;
this->Text = L"My Button!";
this->UseVisualStyleBackColor = true;
this->Click += gcnew System::EventHandler(this, &MyClass::PlayIt);
}
void MyClass::InitializeComboBox(System::Windows::Forms::ComboBox ^%CB)
{
CB = gcnew System::Windows::Forms::ComboBox;
CB->FormattingEnabled = true;
CB->Location = System::Drawing::Point(100, 100);
CB->Size = System::Drawing::Size(121, 21);
CB->TabIndex = 2;
MyCB->Parent = MyClass::Parent;
CB->SelectedIndexChanged += gcnew System::EventHandler(this,
&MyClass::comboBox1_SelectedIndexChanged);
}
void MyClass::PlayIt(System::Object^ sender, System::EventArgs^ e)
{
Drawing::Point P = (dynamic_cast <Button^> (sender))->Location;
MyCB->Location = P;
}
void MyClass::comboBox1_SelectedIndexChanged(System::Object^
System::EventArgs^ e)

{
}
}

sender,
Multiple Inheritance - DNDI

After pressing the button
Keep in touch:
mohammadshakergtr@gmail.com
http://mohammadshakergtr.wordpress.com/
tweet @ZGTRShaker
Go have some fun!

C++ Windows Forms L11 - Inheritance

  • 1.
    C++.NET Windows Forms Course L11-Inheritance MohammadShaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
  • 5.
  • 6.
    Inheritance • Now, let’shave the following class: #pragma once using namespace::System; ref class MyClass { public: MyClass(void); virtual String ^ToString() override; };
  • 7.
    Inheritance #include "StdAfx.h" #include "MyClass.h" MyClass::MyClass(void) {} String^MyClass::ToString() { return "I won't red 3leek :P:P "; };
  • 8.
    Inheritance • What happens? private:System::Void button1_Click(System::Object^ System::EventArgs^ e) { MyClass ^MC = gcnew MyClass; textBox1->Text = MC->ToString(); } sender,
  • 9.
    Inheritance • Let’s getit bigger a little #pragma once using namespace::System; ref class MyClass { public: MyClass(String^ s1, String^ s2); virtual String ^ToString() override; private: String ^FName; String ^LName; };
  • 10.
    Inheritance #include "StdAfx.h" #include "MyClass.h" MyClass::MyClass(String^s1, String^ s2) { FName = s1; LName = s2; } String^ MyClass::ToString() { return String::Format("{0}{1}{2}{3}", "My name is : ", FName, " ", LName); };
  • 11.
    Inheritance • What happens? private:System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { MyClass ^MC = gcnew MyClass("MeMe", "Auf-Wiedersehen"); textBox1->Text = MC->ToString(); }
  • 12.
    Inheritance • Let’s havethe following ref class. • Everything ok? #pragma once ref class MyClass : System::Windows::Forms::Button { public: MyClass(void); }; Compiler error, why?
  • 13.
    Inheritance #pragma once using namespaceSystem; ref class MyClass : System::Windows::Forms::Button { public: MyClass(void); };
  • 14.
    Inheritance • Why doingthis? #pragma once using namespace System; namespace dotNet8_Inher { ref class Form1; Forward declaration ref class MyClass : System::Windows::Forms::Button { public: MyClass(void); }; }
  • 15.
    Inheritance • .cpp file #include"StdAfx.h" #include "MyClass.h" namespace dotNet8_Inher { MyClass::MyClass(void) {} }
  • 16.
    Inheritance • In Form1.h private:System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyClass ^MC = gcnew MyClass; } • Sth needed? for Controls? • In Form1.h private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyClass ^MC = gcnew MyClass; MC->Parent = this; } • What will happen now?
  • 17.
  • 18.
    Inheritance • Now, let’shave the following in cpp file #include "StdAfx.h" #include "MyClass.h" namespace dotNet8_Inher { MyClass::MyClass(void) { this->Text = "I'M HAPPY!"; } }
  • 19.
    Inheritance • Again, inForm1.h private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyClass ^MC = gcnew MyClass; MC ->Parent = this; } • What will happen now?
  • 20.
  • 21.
    #include "StdAfx.h" #include "MyClass.h" namespacedotNet8_Inher { MyClass::MyClass(void) { this->Location = System::Drawing::Point(223, 121); this->Name = L"button1"; this->Size = System::Drawing::Size(75, 23); this->TabIndex = 0; this->Text = L"My Button!"; this->UseVisualStyleBackColor = true; } } private: System::Void Form1_Load(System::Object^ System::EventArgs^ e) { MyClass ^MC = gcnew MyClass; MC ->Parent = this; } sender,
  • 22.
  • 23.
    A way tosteal :D
  • 24.
  • 25.
    Multiple Inheritance • Now,let’s see the following crazy code #pragma once using namespace System; namespace dotNet8_Inheir { ref class Form1; ref class MyClass : System::Windows::Forms::Button, System::Windows::Forms::ComboBox { private: Form1 ^MyForm; public: MyClass(Form1 ^f); }; }
  • 26.
    #include "StdAfx.h" #include "MyClass.h" namespacedotNet8_Inheir { MyClass::MyClass(void) { this->Location = System::Drawing::Point(223, 121); this->Name = L"button1"; this->Size = System::Drawing::Size(75, 23); this->TabIndex = 0; this->Text = L"My Button!"; this->UseVisualStyleBackColor = true; } } private: System::Void Form1_Load(System::Object^ System::EventArgs^ e) { MyClass ^MC = gcnew MyClass; MC ->Parent = this; } sender,
  • 27.
  • 29.
    Multiple Inheritance • Why? –Can’t know the “location” peoperties is for! – Can’t inhert from more than one base class in.NET! • So, what to do? – Dump fix. (Do Not Do it (DNDI) unlsess necessary)
  • 30.
    Multiple Inheritance -DNDI #pragma once using namespace System; namespace dotNet8_Inheir { ref class Form1; ref class MyClass : public System::Windows::Forms::Button { private: Form1 ^MyForm; System::Windows::Forms::ComboBox ^MyCB; public: MyClass(Form1 ^f); void InitializeButton(); void InitializeComboBox(System::Windows::Forms::ComboBox ^%); }; }
  • 31.
    #include "StdAfx.h" #include "MyClass.h” namespacedotNet8_Inheir { MyClass::MyClass(Form1 ^f) { MyForm = f; InitializeButton(); InitializeComboBox(MyCB); } void MyClass::InitializeButton() { this->Location = System::Drawing::Point(223, 121); this->Name = L"button1"; this->Size = System::Drawing::Size(75, 23); this->TabIndex = 0; this->Text = L"My Button!"; this->UseVisualStyleBackColor = true; } void MyClass::InitializeComboBox(System::Windows::Forms::ComboBox ^%CB) { CB = gcnew System::Windows::Forms::ComboBox; CB->Location = System::Drawing::Point(223, 121); CB->Size = System::Drawing::Size(75, 23); CB->TabIndex = 0; CB->Text = L"My ComboBox!"; CB->Parent = this; } }
  • 33.
    Multiple Inheritance -DNDI • Let’s start all over again private: System::Void Form1_Load(System::Object^ System::EventArgs^ e) { MyClass ^MC; MC = gcnew MyClass(this); } sender,
  • 34.
    Multiple Inheritance -DNDI #pragma once using namespace System; namespace dotNet8_Inheir { ref class Form1; ref class MyClass : public System::Windows::Forms::Button { private: Form1 ^MyForm; System::Windows::Forms::ComboBox ^MyCB; public: MyClass(Form1 ^f); void InitializeButton(); void InitializeComboBox(System::Windows::Forms::ComboBox ^%); void PlayIt(System::Object^ sender, System::EventArgs^ e); void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e); }; }
  • 35.
    #include "StdAfx.h" #include "MyClass.h" #include"Form1.h" namespace dotNet8_Inheir { MyClass::MyClass(Form1 ^f) { MyForm = f; this->Parent = MyForm; InitializeButton(); InitializeComboBox(MyCB); } void MyClass::InitializeButton() { this->Button::Location = System::Drawing::Point(223, 121); this->Name = L"button1"; this->Size = System::Drawing::Size(75, 23); this->TabIndex = 0; this->Text = L"My Button!"; this->UseVisualStyleBackColor = true; this->Click += gcnew System::EventHandler(this, &MyClass::PlayIt); }
  • 36.
    void MyClass::InitializeComboBox(System::Windows::Forms::ComboBox ^%CB) { CB= gcnew System::Windows::Forms::ComboBox; CB->FormattingEnabled = true; CB->Location = System::Drawing::Point(100, 100); CB->Size = System::Drawing::Size(121, 21); CB->TabIndex = 2; MyCB->Parent = MyClass::Parent; CB->SelectedIndexChanged += gcnew System::EventHandler(this, &MyClass::comboBox1_SelectedIndexChanged); } void MyClass::PlayIt(System::Object^ sender, System::EventArgs^ e) { Drawing::Point P = (dynamic_cast <Button^> (sender))->Location; MyCB->Location = P; } void MyClass::comboBox1_SelectedIndexChanged(System::Object^ System::EventArgs^ e) { } } sender,
  • 37.
    Multiple Inheritance -DNDI After pressing the button
  • 38.
  • 39.