کامپیوتری ها
3.26K subscribers
4.41K photos
496 videos
476 files
467 links
Download Telegram
✳️ مثالی برای استفاده از typedef در برنامه نویسی C
—------------—
#include <stdio.h>
#include <string.h>

typedef struct Books { // look typedef
char title[50];
char author[50];
char subject[100];
int book_id;
} BOOK;

struct Books2 {
char title[50];
char author[50];
char subject[100];
int book_id;
};

int main( ) {

struct Books2 Book2;
BOOK book;

/* Book2 specification */
strcpy( Book2.title, "definition with struct keyword");


/* book specification */
strcpy( book.title, "definition without struct keyword");

/* print Book2 info */
printf( "Book2 title : %s\n", Book2.title);

/* print book info */
printf( "book title : %s\n", book.title);

return 0;
}
—------------------
🆔 @computeriha
✳️ یک مثال برای گرفتن و آزادسازی جافظه در برنامه نویسی C

—-------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

char name[100];
char *description;

strcpy(name, "Ali");

/* allocate memory dynamically */
description = (char *) malloc( 30 * sizeof(char) );

if( description == NULL ) {
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else {
strcpy( description, "Ali is a computer engineering student.");
}

/* suppose you want to store bigger description */
description = (char *) realloc( description, 100 * sizeof(char) );

if( description == NULL ) {
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else {
strcat( description, "She is in class 10th");
}

printf("Name = %s\n", name );
printf("Description: %s\n", description );

/* release memory using free() function */
free(description);
}
—---------------—
🆔 @computeriha
✳️ خواندن و نوشتن از فایل در C

—------------------------------------------------
#include <stdio.h>

void main() {
FILE *fp;
char buff[255];

// %%%%%%%%%%%%% Write
fp = fopen("g:/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);

// %%%%%%%%%%%%% Read
fp = fopen("g:/test.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );

fgets(buff, 255, (FILE*)fp);
printf("2: %s\n", buff );

fgets(buff, 255, (FILE*)fp);
printf("3: %s\n", buff );
fclose(fp);

}
—----------------------
🆔 @computeriha
✳️ نوشتن تابع بازگشتی در C (فاکتوریل)
—------------------—
#include <stdio.h>

int factorial(unsigned int i) {

if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}

int main() {
int i = 15;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
—----------------------------
🆔 @computeriha
✳️ اولین برنامه cpp

#include <iostream>
using namespace std;
// tozih dar ayande

int main()
{
// cout bejaye printf dar c
cout « "Salam @computeriha"«endl;
return 0;
}

🆔 @computeriha
✳️ تفاوت وجود using namespace std و عدم وجودشو ببینید (cpp)

#include <iostream>

int main()
{
std::cout « "Hello World";
system("pause");
return 0;

}
—---------------------------------------------
#include <iostream>

using namespace std;

int main()
{
cout « "Hello World";
system("pause");
return 0;

}
—---------------------------------—
🆔 @computeriha
✳️ ایجاد رفرنس در cpp
—-------------------
#include <iostream>

using namespace std;

int main ()
{
// declare simple variables
int i;
double d;

// declare reference variables
int& r = i;
double& s = d;

i = 5;
cout « "Value of i : " « i « endl;
cout « "Value of i reference : " « r « endl;

d = 11.7;
cout « "Value of d : " « d « endl;
cout « "Value of d reference : " « s « endl;

return 0;
}
—----------------------------
🆔 @computeriha
✳️ مفهوم کلاس در cpp
—------------------—
#include <iostream>

using namespace std;

class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

int main( )
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout « "Volume of Box1 : " « volume «endl;

// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout « "Volume of Box2 : " « volume «endl;
return 0;
}
—---------------
🆔 @computeriha
کامپیوتری ها
✳️ مفهوم کلاس در cpp —------------------— #include <iostream> using namespace std; class Box { public: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; int main( ) { …
✳️ ارث بری در cpp
—---------------------------------------
#include <iostream>

using namespace std;

// Base class
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};

int main(void)
{
Rectangle Rect;

Rect.setWidth(5);
Rect.setHeight(7);

// Print the area of the object.
cout « "Total area: " « Rect.getArea() « endl;

return 0;
}
—-------------------------------
🆔 @computeriha
✳️ ارث بری چند گانه در cpp
—-------------------------------------
#include <iostream>

using namespace std;

// Base class Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// Base class PaintCost
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};

// Derived class
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};

int main(void)
{
Rectangle Rect;
int area;

Rect.setWidth(5);
Rect.setHeight(7);

area = Rect.getArea();

// Print the area of the object.
cout « "Total area: " « Rect.getArea() « endl;

// Print the total cost of painting
cout « "Total paint cost: $" « Rect.getCost(area) « endl;

return 0;
}
—---------------------------—
🆔 @computeriha
کامپیوتری ها
✳️ ارث بری چند گانه در cpp —------------------------------------- #include <iostream> using namespace std; // Base class Shape class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { …
✳️Function overloading in cpp

—------------------------
#include <iostream>
using namespace std;

class printData
{
public:
void print(int i) {
cout « "Printing int: " « i « endl;
}

void print(double f) {
cout « "Printing float: " « f « endl;
}

void print(char* c) {
cout « "Printing character: " « c « endl;
}
};

int main(void)
{
printData pd;

// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");

return 0;
}
—------------------------------------
🆔 @computeriha
کامپیوتری ها
✳️Function overloading in cpp —------------------------ #include <iostream> using namespace std; class printData { public: void print(int i) { cout « "Printing int: " « i « endl; } void print(double f) { cout « "Printing…
✳️ Operators overloading in C++

—----------------------------------------
#include <iostream>
using namespace std;

class Box
{
public:

double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}

void setBreadth( double bre )
{
breadth = bre;
}

void setHeight( double hei )
{
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Main function for the program
int main( )
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);

// volume of box 1
volume = Box1.getVolume();
cout « "Volume of Box1 : " « volume «endl;

// volume of box 2
volume = Box2.getVolume();
cout « "Volume of Box2 : " « volume «endl;

// Add two object as follows:
Box3 = Box1 + Box2;

// volume of box 3
volume = Box3.getVolume();
cout « "Volume of Box3 : " « volume «endl;

return 0;
}

—------------------------------------—
🆔 @Computeriha
کامپیوتری ها
✳️ Operators overloading in C++ —---------------------------------------- #include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double…
✳️ Polymorphism in C++

—------------------------------—
#include <iostream>
using namespace std;

class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
// pure virtual function
virtual int area() = 0;
};
class Rectangle: public Shape{
public:
Rectangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout « "Rectangle class area :" «endl;
return (width * height);
}
};
class Triangle: public Shape{
public:
Triangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout « "Triangle class area :" «endl;
return (width * height / 2);
}
};
// Main function for the program
int main( )
{
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);

// store the address of Rectangle
shape = &rec;
// call rectangle area.
shape->area();

// store the address of Triangle
shape = &tri;
// call triangle area.
shape->area();

return 0;
}
—------------------------
🆔 @computeriha
کامپیوتری ها
✳️ Polymorphism in C++ —------------------------------— #include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape( int a=0, int b=0) { width = a; height = b; } …
✳️ Data Abstraction Example in C++
—---------—
#include <iostream>
using namespace std;

class Adder{
public:
// constructor
Adder(int i = 0)
{
total = i;
}
// interface to outside world
void addNum(int number)
{
total += number;
}
// interface to outside world
int getTotal()
{
return total;
};
private:
// hidden data from outside world
int total;
};
int main( )
{
Adder a;

a.addNum(10);
a.addNum(20);
a.addNum(30);

cout « "Total " « a.getTotal() «endl;
return 0;
}
—------------------------------------
🆔 @Computeriha
کامپیوتری ها
✳️ Data Abstraction Example in C++ —---------— #include <iostream> using namespace std; class Adder{ public: // constructor Adder(int i = 0) { total = i; } // interface to outside world void addNum(int number)…
✳️ C++ Files and Streams
—---------------------------------------—

#include <fstream>
#include <iostream>
using namespace std;

int main ()
{

char data[100];

// open a file in write mode.
ofstream outfile;
outfile.open("afile.dat");

cout « "Writing to the file" « endl;
cout « "Enter your name: ";
cin.getline(data, 100);

// write inputted data into the file.
outfile « data « endl;

cout « "Enter your age: ";
cin » data;
cin.ignore();

// again write inputted data into the file.
outfile « data « endl;

// close the opened file.
outfile.close();

// open a file in read mode.
ifstream infile;
infile.open("afile.dat");

cout « "Reading from the file" « endl;
infile » data;

// write the data at the screen.
cout « data « endl;

// again read the data from the file and display it.
infile » data;
cout « data « endl;

// close the opened file.
infile.close();

return 0;
}

🆔 @computeriha
کامپیوتری ها
✳️ C++ Files and Streams —---------------------------------------— #include <fstream> #include <iostream> using namespace std; int main () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open("afile.dat"); …
✳️ C++ Exception Handling

—------------------------
#include <iostream>
using namespace std;

double division(int a, int b)
{
if( b == 0 )
{
throw "Division by zero condition!";
}
return (a/b);
}

int main ()
{
int x = 50;
int y = 0;
double z = 0;

try {
z = division(x, y);
cout « z « endl;
}catch (const char* msg) {
cerr « msg « endl;
}

return 0;
}
—-------------------------------------------------
🆔 @computeriha
کامپیوتری ها
✳️ C++ Exception Handling —------------------------ #include <iostream> using namespace std; double division(int a, int b) { if( b == 0 ) { throw "Division by zero condition!"; } return (a/b); } int main () { int x = 50; int y =…
1⃣0⃣ Define New Exceptions in c++
—-------------------
#include <iostream>
#include <exception>
using namespace std;

struct MyException : public exception
{
const char * what () const throw ()
{
return "C++ Exception";
}
};

int main()
{
try
{
throw MyException();
}
catch(MyException& e)
{
std::cout « "MyException caught" « std::endl;
std::cout « e.what() « std::endl;
}
catch(std::exception& e)
{
//Other errors
}
}
—------------------
0⃣1⃣ @computeriha
0⃣1⃣ C++ Dynamic Memory

#include <iostream>
using namespace std;

int main ()
{
double* pvalue = NULL; // Pointer initialized with null
pvalue = new double; // Request memory for the variable

*pvalue = 29494.99; // Store value at allocated address
cout « "Value of pvalue : " « *pvalue « endl;

delete pvalue; // free up the memory.

return 0;
}

1⃣0⃣ @computeriha
1⃣0⃣ Dynamic Memory Allocation for Objects

#include <iostream>
using namespace std;

class Box
{
public:
Box() {
cout « "Constructor called!" «endl;
}
~Box() {
cout « "Destructor called!" «endl;
}
};

int main( )
{
Box* myBoxArray = new Box[4];

delete [] myBoxArray; // Delete array

return 0;
}

—------------------------—
compile and run:

Constructor called!
Constructor called!
Constructor called!
Constructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!

0⃣1⃣ @computeriha
0⃣1⃣ Defining a Namespace c++

—---------------------
#include <iostream>
using namespace std;

// first name space
namespace first_space{
void func(){
cout « "Inside first_space" « endl;
}
}
// second name space
namespace second_space{
void func(){
cout « "Inside second_space" « endl;
}
}
int main ()
{

// Calls function from first name space.
first_space::func();

// Calls function from second name space.
second_space::func();

return 0;
}

—---------------------------------—
1⃣0⃣ @computeriha