Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can we use printf/cout/console.writeline in constructor , will it printed on output screen? in any language c,c++,c# when object is created ?

What I have tried:

Can we use printf/cout/consol.writeline in constructor , will it printed on output screen? in any language c,c++,c#
Posted
Updated 13-Sep-16 4:34am
v3
Comments
[no name] 13-Sep-16 10:00am    
No. C# cannot use printf, cout or consol.writeline. printf and cout are C/C++ functions. C# would use Console.WriteLine as you have to spell it correctly and it's case sensitive. As for the rest of your "question" why don't you simply try it and find out for yourself?
Richard MacCutchan 13-Sep-16 11:42am    
You can, but you should not do it. Constructors should do the minimum amount of work necessary to initialise the object.

1 solution

That's confused, because C doesn't have constructors, so printf is irrelevant.
C++ and C# both have object constructors, and you can happily call the appropriate text output methods in constructors with either language: cout for C++, and Console.WriteLine or Debug.WriteLine for C#.

Do note that unless you are developing a Console based application, you don;t have a "text display" that the user can automatically see - so these methods are of very limited use in modern applications where UI elements such as text boxes and labels are more normally used to communicate with the user.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900