C++ Windows Forms L11 - Inheritance
Upcoming SlideShare
Loading in...5
×
 

C++ Windows Forms L11 - Inheritance

on

  • 262 views

C++ Windows Forms L11 - Inheritance of C++ Windows Forms Light Course

C++ Windows Forms L11 - Inheritance of C++ Windows Forms Light Course

Statistics

Views

Total Views
262
Views on SlideShare
262
Embed Views
0

Actions

Likes
0
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

C++ Windows Forms L11 - Inheritance C++ Windows Forms L11 - Inheritance Presentation Transcript

  • 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!