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

  • Thread starter Thread starter Dr.Brain
  • Start date Start date
  • Tags Tags
    Coding
AI Thread Summary
The discussion revolves around a C++ code snippet that contains errors related to class declarations and syntax. Participants highlight the need for a C++ compiler, as the code uses class structures that are not recognized by C compilers. Key issues identified include missing semicolons after class declarations and incorrect syntax in the output function. The conversation also touches on the differences between C and C++, noting that C lacks classes and constructors, while C++ allows for member functions and constructors within structures. Additionally, it is clarified that C structs cannot contain functions, only function pointers. Overall, the thread emphasizes the importance of using the correct compiler and adhering to proper syntax in C++.
Dr.Brain
Messages
537
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.
 
Back
Top