C# Programming - Classes and Objects - Discussion

Discussion Forum : Classes and Objects - General Questions (Q.No. 3)
3.
Which of the following statements are correct?
  1. Instance members of a class can be accessed only through an object of that class.
  2. A class can contain only instance data and instance member function.
  3. All objects created from a class will occupy equal number of bytes in memory.
  4. A class can contain Friend functions.
  5. A class is a blueprint or a template according to which objects are created.
1, 3, 5
2, 4
3, 5
2, 4, 5
None of these
Answer: Option
Explanation:
No answer description is available. Let's discuss.
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.

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.
(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.

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.


Post your comments here:

Your comments will be displayed after verification.