c++ - Can C have objects? -
c++ - Can C have objects? -
this question has reply here:
can write object-oriented code in c? 33 answersi know 1 of major advantages of c++ programming languages fact can back upwards oop.
example:
#include <iostream> using namespace std; class crectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void crectangle::set_values (int a, int b) { x = a; y = b; } int main () { crectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); homecoming 0; }
i wondering if c supported oop , if how done.
generally, utilize structs , pass context pointer around. direct conversion of example:
#include <stdio.h> struct rectangle { int x, y; }; void rectangle_set_values (struct rectangle * rectangle, int a, int b) { rectangle->x = a; rectangle->y = b; } int rectangle_area (struct rectangle * rectangle) { homecoming rectangle->x * rectangle->y; } int main () { struct rectangle rect; rectangle_set_values(&rect, 3, 4); printf("area: %d", rectangle_area(&rect)); homecoming 0; }
c++ c oop
Comments
Post a Comment