What's wrong in my c coding (constructors)?

  • Thread starter Thread starter Dr.Brain
  • Start date Start date
  • Tags Tags
    Coding
Click For Summary

Discussion Overview

The discussion revolves around issues in C++ coding, specifically regarding the use of classes and constructors in a program that appears to be intended for C++. Participants are examining syntax errors and the differences between C and C++ regarding class functionality.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant points out a "Declaration Syntax Error" in the provided code, suggesting that a semicolon is needed after class declarations.
  • Another participant questions whether the code is being compiled with a C++ compiler or a C compiler.
  • There is a suggestion to change the name 'info' in case it is a reserved word.
  • One participant notes that C does not support classes, indicating that a C compiler will not recognize the class keyword.
  • Another participant mentions that C structs cannot have functions, only function pointers, contrasting this with C++ where structs can have member functions.
  • There is a humorous interjection with a different code snippet that does not compile, illustrating a potential misunderstanding of syntax.
  • A question is raised about whether constructors are exclusive to C++ and not available in C.

Areas of Agreement / Disagreement

Participants generally agree that the code is intended for C++ and that there are syntax issues. However, there is disagreement about the specifics of C versus C++ capabilities, particularly regarding constructors and class functionality.

Contextual Notes

Limitations include the assumption that the reader understands the differences between C and C++ regarding classes and constructors, as well as the unresolved syntax errors in the provided code.

Who May Find This Useful

Individuals learning C++ programming, particularly those transitioning from C, or those seeking to understand class and constructor usage in C++. Also, readers interested in debugging syntax errors in C++ code.

Dr.Brain
Messages
539
Reaction score
2
#include<stdio.h>
#include<conio.h>
class info
{
int name,roll;
public:
void input();
void output();
}
void info::input()
{
printf("Enter your name and rollno \n");
scanf("%c%d" ,&name,&roll);
}
void info::output()
{
printf("Your name is %c \n" ,name);
printf("Your rollno is &d \n" , roll);
}
class marks :public info
{
int math,bio,eng;
public:
void input();
void output();
}
void marks::input()
{
info::input() ;
printf("Input marks : \n");
scanf("%d%d%d" , &math , &bio , &eng);
}
void marks::output()
{
info::ouput();
printf("Your marks are %d %d %d \n" ,math,bio,eng);
}
void main()
{
clrscr();
class marks m;
m.input();
m.output();
getch();
}

---------------

I have darkened the line in which my compiler shows the error...i don't seem to find any ... :bugeye: , it says "Declaration Syntax Error")
 
Technology news on Phys.org
Are you using a C++ compiler or a C compiler?
 
Is 'info' a reserved word? Try changing it.
 
Put a semicolon after the class declarations:
Code:
class info
{
int name,roll;
public:
void input();
void output();
};
etc.

You need a C++ compiler for this.
 
heh, my program would look like this:

Code:
using namespace std;
while(Subject.GetNext())
{
   cout >> "You'r Grade for ">> Subject.Get() >> " is ";
   cout >> "F!" >> endl;
}
 
Last edited:
3trQN said:
heh, my program would look like this:

Code:
using namespace std;
while(Subject.GetNext())
{
   cout >> "You'r Grade for ">> Subject.Get() >> " is "
   cout >> "F!" >> endl;
}

Too bad it won't compile.
 
do constructors worl only in C++ and not C?
 
C does not have classes. A C compiler will not recognize the class keyword.

C does have structures. In C++, a structure can have a constructor as well as member functions. I do not know whether structs in C can have constructors or other member functions.

Good luck,
Tim
 
C structs(different from C++ structs) cannot have functions...they can have function pointers but not functions.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
14
Views
4K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
Replies
89
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K