C# Programming - Classes and Objects - Discussion
Discussion Forum : Classes and Objects - General Questions (Q.No. 3)
3.
Which of the following statements are correct?
- Instance members of a class can be accessed only through an object of that class.
- A class can contain only instance data and instance member function.
- All objects created from a class will occupy equal number of bytes in memory.
- A class can contain Friend functions.
- A class is a blueprint or a template according to which objects are created.
Discussion:
11 comments Page 1 of 2.
Arun said:
10 months ago
According to me, 1, 5 is the correct options.
Alek said:
9 years ago
Statement 3 is incorrect, obvious example is system string class:
string s1 = new string ('a', 1) ;
string s2 = new string ('a', 1000) ;
s1 occupies 2 bytes, while s2 occupies 2000 bytes.
string s1 = new string ('a', 1) ;
string s2 = new string ('a', 1000) ;
s1 occupies 2 bytes, while s2 occupies 2000 bytes.
Gloops said:
9 years ago
public class example
{
public static string use = "demonstrate class field";
public int nb;
}
public class program
{
public static void main(string[] args)
{
example e = new example();
e.nb = 3;
Console.WriteLine(example.use);
Console.WriteLine(e.nb);
}
}
}
e is an instance of the class example.
use is a class field, you recognize it with the keyword static. From the program you access it by prefixing it with the name of the class :
example.use
nb is an instance field, you access it by prefixing it with the name of the instance :
e.nb
If you declare three example objects you have three different values of nb, but only one value of use for it is a static field of the class.
{
public static string use = "demonstrate class field";
public int nb;
}
public class program
{
public static void main(string[] args)
{
example e = new example();
e.nb = 3;
Console.WriteLine(example.use);
Console.WriteLine(e.nb);
}
}
}
e is an instance of the class example.
use is a class field, you recognize it with the keyword static. From the program you access it by prefixing it with the name of the class :
example.use
nb is an instance field, you access it by prefixing it with the name of the instance :
e.nb
If you declare three example objects you have three different values of nb, but only one value of use for it is a static field of the class.
(1)
L SIDHARTH said:
9 years ago
What is difference b/w class variables and instance variable?
L SIDHARTH said:
9 years ago
What is instance member?
Ramesh Malode said:
1 decade ago
Instance member can be directly accessed by class name if its static. So the first option is not correct.
Ramesh Malode said:
1 decade ago
You can also use 'protected internal' where you restrict access to the current assembly or types derived from the containing class.
Shubhdeep Singh said:
1 decade ago
Instance member can be directly accessed by class name if its static. So the first option is improper.
Saurabh Sharma said:
1 decade ago
There is no friend function in c# but you can use the 'internal' access modifier which will make this class accessible to the other classes in the same assembly, but not accessible outside the assembly.
Alternatively you can also use 'protected internal' where you restrict access to the current assembly or types derived from the containing class.
Alternatively you can also use 'protected internal' where you restrict access to the current assembly or types derived from the containing class.
Rajeev said:
1 decade ago
I think we can access Instance member of a class using inheritance;.
So option 1 is wrong.
You need to check it out.
So option 1 is wrong.
You need to check it out.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers