As we learn all, C dont supprot Inheretic, Polymorphism, Function Overloading... And Few Other Things...

My Question is:

What is this one?

[IMG][/IMG]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Example
{
    public partial class WForm : Form
    {
        public WForm()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            
            Student _ST = new Student(1234, "History", 10.0f);
            _ST.Write();
            this.Controls.Add(_ST.RTB);
        }
    }

    // :Form To Use RichTextBox...s
    class Student : Form
    { 
        // Variables
        private int AM;
        private string Lesson;
        private float Grade;
        public RichTextBox RTB;

        public Student(int kAM, string kLess, float kGrade)      
        {
            AM = kAM;     
            Lesson = kLess; 
            Grade = kGrade;
        }
  
        public Student(int JustAM)
        {
            AM = JustAM;    
        }

        public void Write()
        {
            RTB = new RichTextBox();
            //Styling...
            RTB.Dock = DockStyle.Fill;
            RTB.BackColor = Color.CornflowerBlue;
            RTB.ForeColor = Color.White;
            RTB.Font = new Font("Arial", 12);

            RTB.Text = "Version 1: (1234, History, 10.0f)\n"
            + "\nAM: " + AM + "\nLesson: " + Lesson + "\nGrade: " + Grade + "\n\n";
        }
    }

}

Dani AI

Generated

Short summary and practical fixes.

The posted C# code works because Student inherits Form and creates a RichTextBox inside itself — that makes each Student object also a window. That is valid, but it mixes UI and data. As and hinted, C# is fully object‑oriented (like C++), and allocation/ownership rules differ from plain C.

Why Base.A = 10; failed

  • In C# you cannot put statements directly at class scope. Assignments must be inside a constructor, method, or a field initializer.
  • Base.A refers to a static member on the type; if A is an instance field you should just assign A = 10; inside a constructor or method. If you want the base constructor to set it, call : base(...) from the derived constructor.

Example (C# — correct patterns):

class Base { protected int A; public Base(int a) { A = a; } }
class Derived : Base
{
    public Derived() : base(10) { }    // call base ctor
    public void Init() { A = 10; }     // or assign inside a method/ctor
}

Design tip — prefer composition over inheritance for UI
Keep the student data separate from the form. Make a plain data class and a small form/control that takes that data and displays it. That keeps logic testable and avoids odd coupling between domain objects and WinForms.

Polymorphism in brief
Both C# and C++ support polymorphism using virtual/override (and interfaces/abstract classes). Example (C#):

class Animal { public virtual string Speak() => "..." ; }
class Dog : Animal { public override string Speak() => "woof"; }

Practical checklist: put statements inside constructors/methods, use protected or properties rather than public fields, call base(...) when you want the base ctor to initialize state, and separate model from view for cleaner code. ’s suggestion to assign inside a method/ctor is precisely the correct quick fix.

Recommended Answers

All 8 Replies

It's a window with a rich text box.

I'm not sure what you are trying to ask here.

It's a window with a rich text box.

I'm not sure what you are trying to ask here.

Brother, i coded it ;p so i know that is a rtb ;p

just look code and explain me that part

// In C++ it can be [ Student ST(1234, "Histoyry", 10.0f); ]
Student _ST = new Student(1234, "History", 10.0f);

Look and here

public Student(int kAM, string kLess, float kGrade)
{
AM = kAM;
Lesson = kLess;
Grade = kGrade;
}

this one in C++ is like this

class Student
{
 Student(int kam, string kless, float kgrade)
   :AM(kam),Lesson(kless),Grade(kgrade)
    {  }
}

all this dont look like polymorphism? but as we know, only c++ support polymorphish ...

C# supports both polymorphism and inheritance, as it is an object oriented language.

Of the three (C, C#, C++) only C isn't an OO language.

You are saying C does not support object oriented and show C# code ?!!

This is like showing tasting a cake and saying that the meat is tasty.

Well, C# is not C... At beggin i confused a bit because in Visual Studio there is not any option for C... anyway

C# Is part of C
C++ is part of C

well i think C# dont support Inheretic/polymorphism... anyway

#Tellacla
Yea my mistake, some time i forgot that C# is not C ;ppp

But if you read any book for C++ it say that "The good with c++ is that c++ support inheretic/polymorphism,encapsulation and others no"

soz for my bad engl

Well, C# is not C... At beggin i confused a bit because in Visual Studio there is not any option for C... anyway

C# Is part of C
C++ is part of C

well i think C# dont support Inheretic/polymorphism... anyway

#Tellacla
Yea my mistake, some time i forgot that C# is not C ;ppp

But if you read any book for C++ it say that "The good with c++ is that c++ support inheretic/polymorphism,encapsulation and others no"

soz for my bad engl

Don't think of the languages as part of each other. The compilers for them are different and even though they may share some of the same syntax, only C/C++ are closely related (a C++ compiler will compile C code, albeit with some warnings of depreciation perhaps).

C# is very different and definitely does support inheritance (and by extension Polymorphism). public class Dog : Animal { } The comment you put for the construction of an object...In C++ you have, effectively, two ways of instantiating and object. MyObject obj = MyObject(someVal); In this case, the obj variable contains all the information about MyObject, so if you were to pass this variable into methods around the program, you would end up moving a fair amount of data around (a copy would be made every time you passed this object to a method)

The second way is MyObject* obj = new MyObject(someVal); Here you create a pointer to the object. A pointer simply "points" to where the object data is in memory and is the cheapest way of passing objects to methods and classes. All that happens in this case, is the memory location of where your object sits in memory, is passed to your methods and classes. However, this has the intentional/unintentional side effect of allowing you to modify your class data from anywhere in your application which accepts a pointer of this type.

To relate this to C#, nearly all objects are created as this "pointer" type, but the runtime deals with all the management of the memory so you don't have to worry about it (the Garbage Collector) and whether or not they are a reference type or a value type.

commented: Well said! +14

Hmm, i will try again Inheretic...

I had something like

public class Base
{
  public int A;
}

public class Derived: Base
{
   Base.A = 10;
}

But i get error, when i type Base. i dont see functions/variables

You cannot initialise a field that way, but this is perfectly legal:

public class Base
    {  
        public int A;
    }
    
    public class Derived: Base
    {
        public void MyMethod()
        {
            A = 10;
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.