Beruflich Dokumente
Kultur Dokumente
C++ (vs) C#
In C++, the class definition syntax should end with ; whereas in C#, its not
required.
OOP Development
i) Class Definition Syntax:
class classname { //Data members go here //Methods go here }
ii)
Hour 14 - Page 1 of 4
iii)
iv)
Access Modifiers
a) private: This is the default access modifier. The private member is
accessible only inside of the self class. It won't be accessible in the other classes. In other words, it offers limited accessibility. b) public: The public member is accessible inside of the self class and also in other classes. In other words, it offers un-limited accessibility. c) protected: The protected member is accessible inside of the self class and also in relevant sub classes. It is meant for only "Inheritance".
v)
Functional Modifiers
a) static:
The static members are accessible without creating any
object. Those can be accessible with the class name. Syn: classname.membername b) readonly: client code. This functional modifier can be used with data members
only. The value of the readonly data members cant be changed from the
vi)
Hour 14 - Page 2 of 4
vii)
namespace OOPDemo { class MyClass { //data member public string MyDataMember = "This is my sample data member."; //method public void MyMethod() { Console.WriteLine("This is my sample method."); } } class Program { static void Main(string[] args) { //construct an object MyClass mc = new MyClass(); //access data member Console.WriteLine(mc.MyDataMember); //access method mc.MyMethod(); Console.Read(); } } } Output:
Hour 14 - Page 3 of 4
namespace FunctionalModifiersDemo { class Sample { //read only data member public readonly string CompanyName = "Wirpo"; //static data member public static string Location = "Hyderabad"; //static method public static void ShowAddress() { Console.WriteLine("Hi-Tech City, Madapur."); } } class Program { static void Main(string[] args) { Sample s = new Sample(); Console.WriteLine(s.CompanyName); //s.CompanyName = "TCS"; // is not allowed bcoz it is the readonly member. Sample.ShowAddress(); Console.WriteLine(Sample.Location); Console.Read(); } } } Output:
Hour 14 - Page 4 of 4